Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(cloud): D2 Nachbesserung 7 - nur netzfoermige Rohfehler umetikettieren, Original als cause

purple5b4cadb084e9
diff --git a/src/api/cloud/__tests__/cloud-client.test.ts b/src/api/cloud/__tests__/cloud-client.test.ts
index 7d1718d5..355964ff 100644
--- a/src/api/cloud/__tests__/cloud-client.test.ts
+++ b/src/api/cloud/__tests__/cloud-client.test.ts
@@ -225,6 +225,42 @@ describe('cloudFetch', () => {
expect(err).not.toBeInstanceOf(CloudJobError)
})
+ it('D2 Nachbesserung 7: the original error survives as `cause`, for the log', async () => {
+ const original = new TypeError('Failed to fetch')
+ fetchMock.mockRejectedValue(original)
+ const err = await cloudFetch('/api/account/content-policy').catch((e: unknown) => e)
+ expect(err).toBeInstanceOf(CloudJobError)
+ expect((err as CloudJobError).cause).toBe(original)
+ })
+
+ it('D2 Nachbesserung 7: a genuine bug in this file is NOT relabelled as a server outage', async () => {
+ // The original condition caught ANY bare rejection, so a programming
+ // error thrown from inside cloudFetch's own try block — a broken
+ // response parser, a bad property access — came back saying "LU Cloud
+ // server unreachable" forever, hiding a real bug behind the wrong
+ // explanation. A TypeError whose message is not one of the network-shaped
+ // ones must pass through completely unchanged.
+ const bug = new TypeError("Cannot read properties of undefined (reading 'foo')")
+ fetchMock.mockRejectedValue(bug)
+ const err = await cloudFetch('/api/account/content-policy').catch((e: unknown) => e)
+ expect(err).toBe(bug)
+ expect(err).not.toBeInstanceOf(CloudJobError)
+ })
+
+ it.each([
+ ['Chromium/WebView2', 'TypeError', 'Failed to fetch'],
+ ['Firefox', 'TypeError', 'NetworkError when attempting to fetch resource.'],
+ ['Safari/WebKit', 'TypeError', 'Load failed'],
+ ['Node/undici (npm run dev)', 'TypeError', 'fetch failed'],
+ ['Rust-side client', 'Error', 'error sending request for url'],
+ ])('D2 Nachbesserung 7: %s\'s own network-failure wording is recognized', async (_engine, ctor, message) => {
+ const ErrCtor = ctor === 'TypeError' ? TypeError : Error
+ fetchMock.mockRejectedValue(new ErrCtor(message))
+ const err = await cloudFetch('/api/account/content-policy').catch((e: unknown) => e)
+ expect(err).toBeInstanceOf(CloudJobError)
+ expect((err as CloudJobError).message).toMatch(/could not reach/i)
+ })
+
it('preserves method, body and extra headers', async () => {
fetchMock.mockResolvedValue(jsonRes({}))
await cloudFetch('/api/jobs', {
diff --git a/src/api/cloud/client.ts b/src/api/cloud/client.ts
index b6ded899..652227ed 100644
--- a/src/api/cloud/client.ts
+++ b/src/api/cloud/client.ts
@@ -18,8 +18,17 @@ export class CloudJobError extends Error {
/** What `retry-after` asked for, in ms — the burst guard's window is fixed
* and up to a minute long, so the number is worth showing. */
readonly retryAfterMs?: number
- constructor(message: string, status: number, meta?: { code?: string; retryAfterMs?: number }) {
- super(message)
+ constructor(
+ message: string,
+ status: number,
+ meta?: { code?: string; retryAfterMs?: number; cause?: unknown },
+ ) {
+ // Opus-Review Nachbesserung 7 (3.0.1, D2): the reworded network-failure
+ // message below intentionally loses the engine's own text — `cause`
+ // keeps it reachable for a log line without putting it back in front of
+ // the customer. `super(message)` alone (no options) when nothing is
+ // passed, matching every existing call site's behavior exactly.
+ super(message, meta && 'cause' in meta ? { cause: meta.cause } : undefined)
this.name = 'CloudJobError'
this.status = status
this.code = meta?.code
@@ -123,6 +132,22 @@ export interface CloudFetchInit extends RequestInit {
timeoutMs?: number
}
+/**
+ * Opus-Review Nachbesserung 7 (3.0.1, D2): the closed set of raw rejection
+ * MESSAGES the fetch spec's own engines use for "this request never reached
+ * anything" — connection refused, DNS failure, CORS block, TLS failure. Not
+ * a generic `err instanceof TypeError` check: a broken response parser or a
+ * bad property access inside this app's own code also throws a TypeError,
+ * and relabelling THAT as "LU Cloud server unreachable" would hide a real
+ * bug behind a server-side explanation forever.
+ */
+const NETWORK_SHAPED_MESSAGE =
+ /Failed to fetch|NetworkError|Load failed|fetch failed|error sending request/i
+
+function looksLikeRawNetworkFailure(err: unknown): boolean {
+ return err instanceof Error && NETWORK_SHAPED_MESSAGE.test(err.message)
+}
+
/** Settle `work` as soon as it settles, or reject the moment `signal` aborts.
*
* For the steps of a request that take no AbortSignal of their own. The
@@ -202,13 +227,28 @@ export async function cloudFetch(path: string, init: CloudFetchInit = {}): Promi
// and keeps its own accurate message; `callerAborted` is a deliberate
// cancel (Stop, a run tearing down), not a failure, and every existing
// caller already relies on a cancel staying a plain, non-CloudJobError
- // rejection to tell the two apart. Only a bare, un-typed rejection that
- // is NEITHER of those — a genuine network failure — gets reworded into
- // something a customer can act on.
- if (!callerAborted && !(err instanceof CloudJobError)) {
+ // rejection to tell the two apart.
+ //
+ // Opus-Review Nachbesserung 7: the ORIGINAL condition reworded EVERY bare
+ // rejection, including a genuine programming error that happens to throw
+ // inside this try block (a TypeError from a broken response parser, a bad
+ // property access) — a bug in this app's own code would come back
+ // relabelled as "LU Cloud server unreachable" forever, and the next
+ // report would chase the wrong half. `looksLikeRawNetworkFailure` only
+ // matches the small, closed set of messages the fetch spec and its
+ // engines actually use for "the request never reached anything": Chromium
+ // WebView2/Chrome ("Failed to fetch"), Firefox ("NetworkError when
+ // attempting to fetch"), Safari/WebKit ("Load failed"), and the Rust-side
+ // client this same request path uses under `npm run dev`/Node
+ // ("fetch failed" / "error sending request"). Anything else — including
+ // an error this app's own code raised — passes through unchanged. The
+ // original message still reaches the log, as `cause`, even though the
+ // customer only ever sees the actionable rewording.
+ if (!callerAborted && !(err instanceof CloudJobError) && looksLikeRawNetworkFailure(err)) {
throw new CloudJobError(
'Could not reach the LU Cloud server. This can happen right after an app update while the server catches up — try again in a moment, or check for a newer version.',
0,
+ { cause: err },
)
}
throw err