fix(frontend): show a friendly message when the backend is unreachable
request()'s fetch() call throwing (network down, connection refused, offline) previously propagated the raw browser error text (e.g. "TypeError: Failed to fetch"). Centralizing the friendly message here, rather than in each call site, means every existing catch((e) => showError(String(e))) pattern gets it for free.
This commit is contained in:
@@ -16,11 +16,22 @@ import type {
|
|||||||
export const BASE_URL = import.meta.env.VITE_API_BASE_URL ?? "http://localhost:8080";
|
export const BASE_URL = import.meta.env.VITE_API_BASE_URL ?? "http://localhost:8080";
|
||||||
|
|
||||||
async function request<T>(path: string, init?: RequestInit): Promise<T> {
|
async function request<T>(path: string, init?: RequestInit): Promise<T> {
|
||||||
const res = await fetch(`${BASE_URL}${path}`, {
|
let res: Response;
|
||||||
|
try {
|
||||||
|
res = await fetch(`${BASE_URL}${path}`, {
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
credentials: "include",
|
credentials: "include",
|
||||||
...init,
|
...init,
|
||||||
});
|
});
|
||||||
|
} catch {
|
||||||
|
// fetch() itself rejecting (not a non-2xx response, which is handled
|
||||||
|
// below) means the backend genuinely isn't reachable -- offline,
|
||||||
|
// connection refused, CORS, etc. The raw browser error text here
|
||||||
|
// (e.g. "TypeError: Failed to fetch") is not something to show a user;
|
||||||
|
// every caller's catch-and-showError already displays whatever this
|
||||||
|
// throws, so the friendly text only needs to be written once, here.
|
||||||
|
throw new Error("Can't reach the server — check your connection.");
|
||||||
|
}
|
||||||
if (res.status === 401 && path !== "/api/session/me") {
|
if (res.status === 401 && path !== "/api/session/me") {
|
||||||
// An established session expired mid-use (not the initial "am I logged
|
// An established session expired mid-use (not the initial "am I logged
|
||||||
// in" check, which LoginGate itself interprets) -- reload so LoginGate
|
// in" check, which LoginGate itself interprets) -- reload so LoginGate
|
||||||
|
|||||||
Reference in New Issue
Block a user