From d19e32ccadbeabf401e1134768957558b140cdb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Sun, 29 Mar 2026 10:30:22 +0200 Subject: [PATCH] Use duck-typing for Request check and make clarification best-effort MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace instanceof Request with a duck-type isRequestLike helper to avoid cross-realm issues, and wrap the clarification logic in a try/catch so it can never mask the original error. Co-Authored-By: Claude Opus 4.6 (1M context) Signed-off-by: Fredrik Adelöw --- .../ClarifyFailuresFetchMiddleware.ts | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/packages/core-app-api/src/apis/implementations/FetchApi/ClarifyFailuresFetchMiddleware.ts b/packages/core-app-api/src/apis/implementations/FetchApi/ClarifyFailuresFetchMiddleware.ts index 46985bb244..8f63452454 100644 --- a/packages/core-app-api/src/apis/implementations/FetchApi/ClarifyFailuresFetchMiddleware.ts +++ b/packages/core-app-api/src/apis/implementations/FetchApi/ClarifyFailuresFetchMiddleware.ts @@ -29,22 +29,36 @@ export class ClarifyFailuresFetchMiddleware implements FetchMiddleware { return await next(input as any, init); } catch (e) { if (e instanceof TypeError && e.message === 'Failed to fetch') { - let method: string; - let url: string; + try { + let method: string; + let url: string; - if (input instanceof Request) { - method = input.method; - url = input.url; - } else { - method = init?.method || 'GET'; - url = String(input); + if (isRequestLike(input)) { + method = input.method; + url = input.url; + } else { + method = init?.method || 'GET'; + url = String(input); + } + + e.message = `Failed to fetch: ${method} ${url}`; + } catch { + // intentionally ignored - clarification is best-effort } - - e.message = `Failed to fetch: ${method} ${url}`; - throw e; } throw e; } }; } } + +function isRequestLike(input: unknown): input is Request { + return ( + input !== null && + typeof input === 'object' && + 'method' in input && + 'url' in input && + typeof input.method === 'string' && + typeof input.url === 'string' + ); +}