fix(pos): handle expired API sessions and add receipt preview close button
This commit is contained in:
@@ -12,6 +12,15 @@ async function apiFetch<T>(path: string, init?: RequestInit): Promise<T> {
|
||||
...(init?.headers ?? {}),
|
||||
},
|
||||
});
|
||||
if (res.status === 401) {
|
||||
if (typeof window !== 'undefined') {
|
||||
window.location.href = '/login';
|
||||
}
|
||||
throw Object.assign(new Error('Sesión caducada, identifícate de nuevo'), {
|
||||
status: 401,
|
||||
code: 'UNAUTHORIZED',
|
||||
});
|
||||
}
|
||||
if (!res.ok) {
|
||||
const err = (await res.json().catch(() => ({ message: res.statusText }))) as {
|
||||
message?: string;
|
||||
@@ -22,6 +31,15 @@ async function apiFetch<T>(path: string, init?: RequestInit): Promise<T> {
|
||||
code: err.error?.code,
|
||||
});
|
||||
}
|
||||
// Defensive: if a proxy/misconfiguration ever returns HTML on a successful
|
||||
// response, surface a readable message instead of a JSON parse crash.
|
||||
const contentType = res.headers.get('content-type') ?? '';
|
||||
if (!contentType.includes('application/json')) {
|
||||
throw Object.assign(new Error('Respuesta inesperada del servidor (sesión o proxy)'), {
|
||||
status: res.status,
|
||||
code: 'INVALID_RESPONSE',
|
||||
});
|
||||
}
|
||||
return res.json() as Promise<T>;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user