Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion frontend/app/src/shared/api/rest/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ export const fetchUrl = async (url: string, payload?: RequestInit) => {

const rawResponse = await fetch(url, newPayload);

return rawResponse?.json();
if (!rawResponse.ok) {
throw new Error(`Request failed with status ${rawResponse.status}`);
}

return rawResponse.json();
Comment on lines +25 to +29
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Add tests for the new non-OK behavior of fetchUrl.

This introduces new control flow and error shape behavior, but no tests are included. Please add coverage for: (1) non-OK JSON error body and (2) non-OK non-JSON body fallback.

As per coding guidelines, "Write tests for new functionality in frontend code".

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@frontend/app/src/shared/api/rest/fetch.ts` around lines 25 - 29, Add unit
tests for fetchUrl to cover the new non-OK control flow: (1) mock a non-OK
Response whose body is JSON (e.g., {error: "msg"}) and assert fetchUrl throws an
Error containing/parsing that JSON error shape, and (2) mock a non-OK Response
whose body is non-JSON (e.g., plain text) and assert fetchUrl falls back to a
generic error message including the HTTP status; use the same test
harness/utilities used elsewhere to mock global fetch and import the fetchUrl
function to locate the behavior in fetch.ts.

⚠️ Potential issue | 🟠 Major

Preserve error payload contract when throwing on non-OK responses.

This now throws a plain Error, but frontend/app/src/pages/auth-callback.tsx (Line 41) reads error.errors. That field is lost, so backend validation/auth errors won’t propagate correctly.

💡 Proposed fix (keep status check + preserve backend error details)
   if (!rawResponse.ok) {
-    throw new Error(`Request failed with status ${rawResponse.status}`);
+    const error = new Error(
+      `Request failed with status ${rawResponse.status}`
+    ) as Error & { status: number; errors?: unknown };
+    error.status = rawResponse.status;
+
+    try {
+      const errorBody = await rawResponse.json();
+      if (errorBody && typeof errorBody === "object" && "errors" in errorBody) {
+        error.errors = (errorBody as { errors?: unknown }).errors;
+      }
+    } catch {
+      // ignore non-JSON error response bodies
+    }
+
+    throw error;
   }

   return rawResponse.json();
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (!rawResponse.ok) {
throw new Error(`Request failed with status ${rawResponse.status}`);
}
return rawResponse.json();
if (!rawResponse.ok) {
const error = new Error(
`Request failed with status ${rawResponse.status}`
) as Error & { status: number; errors?: unknown };
error.status = rawResponse.status;
try {
const errorBody = await rawResponse.json();
if (errorBody && typeof errorBody === "object" && "errors" in errorBody) {
error.errors = (errorBody as { errors?: unknown }).errors;
}
} catch {
// ignore non-JSON error response bodies
}
throw error;
}
return rawResponse.json();
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@frontend/app/src/shared/api/rest/fetch.ts` around lines 25 - 29, The non-OK
branch in fetch.ts currently throws a plain Error and discards the backend error
payload; instead, read and parse the response body (await rawResponse.json())
before throwing so the thrown value preserves the backend error contract (e.g.,
include the parsed body and status), then throw an object or Error that contains
the parsed payload (so callers like auth-callback.tsx can access error.errors)
while keeping the existing status check around rawResponse.ok and returning
parsed JSON on success.

};

const QSP_TO_INCLUDE = [QSP.BRANCH, QSP.DATETIME];
Expand Down
Loading