fix: expired token issue
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user