Skip to content

Commit 98ce153

Browse files
authored
Merge pull request #31 from obilodeau/harden-frontend-errors
Back-end errors will be better handled by the frontend
2 parents 48ac451 + c4f503a commit 98ce153

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

frontend/src/api.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,20 @@ async function request<T = unknown>(
2929
}
3030
const res = await fetch(`${BASE}${path}`, opts);
3131
const text = await res.text();
32-
const data = text ? (JSON.parse(text) as unknown) : null;
32+
let data: unknown = null;
33+
try {
34+
data = text ? JSON.parse(text) : null;
35+
} catch {
36+
// Non-JSON body (e.g. HTML error page from the dev proxy or a crashed
37+
// backend). Surface the HTTP status + a snippet so it's actually
38+
// debuggable instead of a bare SyntaxError from JSON.parse.
39+
throw new Error(
40+
`${method} ${path}${res.status} ${res.statusText}: ${text.slice(0, 200)}`,
41+
);
42+
}
3343
if (!res.ok) {
3444
const msg = (data as { error?: string } | null)?.error ?? res.statusText;
35-
throw new Error(msg);
45+
throw new Error(`${method} ${path}${res.status}: ${msg}`);
3646
}
3747
return data as T;
3848
}

0 commit comments

Comments
 (0)