fix: expired token issue

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-07-05 11:36:20 -04:00
parent 502be95d7d
commit 6931dc9ff7
5 changed files with 33 additions and 23 deletions
+13 -2
View File
@@ -22,6 +22,14 @@ export interface RequestCallOptions {
headers?: Record<string, string>;
}
/** Error thrown for non-2xx responses, carrying the HTTP status code */
export class FetchError extends Error {
constructor(message: string, public readonly status: number) {
super(message);
this.name = 'FetchError';
}
}
function getCsrfToken(): string | null {
if (typeof document === 'undefined') return null;
const cookie = document.cookie.split('; ').find(r => r.startsWith('X-CSRF-TOKEN='));
@@ -61,8 +69,11 @@ export function createFetchWrapper(options: FetchWrapperOptions = {}) {
if (!response.ok) {
const text = await response.text();
const data = text ? JSON.parse(text) : null;
throw new Error(data?.message || response.statusText);
let data = null;
try {
data = text ? JSON.parse(text) : null;
} catch { /* non-JSON error body */ }
throw new FetchError(data?.message || response.statusText, response.status);
}
if (callOptions?.onStream) {