mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-04-28 18:36:23 +02:00
fix: improve error handling for unauthorized responses and response parsing in BaseApiService
This commit is contained in:
parent
61f220b862
commit
7ac7cd5f99
1 changed files with 51 additions and 47 deletions
|
|
@ -129,20 +129,24 @@ class BaseApiService {
|
||||||
throw new AppError("Failed to parse response", response.status, response.statusText);
|
throw new AppError("Failed to parse response", response.status, response.statusText);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle 401 first before other error handling - ensures token is cleared and user redirected
|
||||||
|
if (response.status === 401) {
|
||||||
|
handleUnauthorized();
|
||||||
|
throw new AuthenticationError(
|
||||||
|
typeof data === "object" && "detail" in data
|
||||||
|
? data.detail
|
||||||
|
: "You are not authenticated. Please login again.",
|
||||||
|
response.status,
|
||||||
|
response.statusText
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// For fastapi errors response
|
// For fastapi errors response
|
||||||
if (typeof data === "object" && "detail" in data) {
|
if (typeof data === "object" && "detail" in data) {
|
||||||
throw new AppError(data.detail, response.status, response.statusText);
|
throw new AppError(data.detail, response.status, response.statusText);
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (response.status) {
|
switch (response.status) {
|
||||||
case 401:
|
|
||||||
// Use centralized auth handler for 401 responses
|
|
||||||
handleUnauthorized();
|
|
||||||
throw new AuthenticationError(
|
|
||||||
"You are not authenticated. Please login again.",
|
|
||||||
response.status,
|
|
||||||
response.statusText
|
|
||||||
);
|
|
||||||
case 403:
|
case 403:
|
||||||
throw new AuthorizationError(
|
throw new AuthorizationError(
|
||||||
"You don't have permission to access this resource.",
|
"You don't have permission to access this resource.",
|
||||||
|
|
@ -157,52 +161,52 @@ class BaseApiService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// biome-ignore lint/suspicious: Unknown
|
// biome-ignore lint/suspicious: Unknown
|
||||||
let data;
|
let data;
|
||||||
const responseType = mergedOptions.responseType;
|
const responseType = mergedOptions.responseType;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
switch (responseType) {
|
switch (responseType) {
|
||||||
case ResponseType.JSON:
|
case ResponseType.JSON:
|
||||||
data = await response.json();
|
data = await response.json();
|
||||||
break;
|
break;
|
||||||
case ResponseType.TEXT:
|
case ResponseType.TEXT:
|
||||||
data = await response.text();
|
data = await response.text();
|
||||||
break;
|
break;
|
||||||
case ResponseType.BLOB:
|
case ResponseType.BLOB:
|
||||||
data = await response.blob();
|
data = await response.blob();
|
||||||
break;
|
break;
|
||||||
case ResponseType.ARRAY_BUFFER:
|
case ResponseType.ARRAY_BUFFER:
|
||||||
data = await response.arrayBuffer();
|
data = await response.arrayBuffer();
|
||||||
break;
|
break;
|
||||||
// Add more cases as needed
|
// Add more cases as needed
|
||||||
default:
|
default:
|
||||||
data = await response.json();
|
data = await response.json();
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Failed to parse response as JSON:", error);
|
|
||||||
throw new AppError("Failed to parse response", response.status, response.statusText);
|
|
||||||
}
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Failed to parse response as JSON:", error);
|
||||||
|
throw new AppError("Failed to parse response", response.status, response.statusText);
|
||||||
|
}
|
||||||
|
|
||||||
// Validate response
|
// Validate response
|
||||||
if (responseType === ResponseType.JSON) {
|
if (responseType === ResponseType.JSON) {
|
||||||
if (!responseSchema) {
|
if (!responseSchema) {
|
||||||
return data;
|
|
||||||
}
|
|
||||||
const parsedData = responseSchema.safeParse(data);
|
|
||||||
|
|
||||||
if (!parsedData.success) {
|
|
||||||
/** The request was successful, but the response data does not match the expected schema.
|
|
||||||
* This is a client side error, and should be fixed by updating the responseSchema to keep things typed.
|
|
||||||
* This error should not be shown to the user , it is for dev only.
|
|
||||||
*/
|
|
||||||
console.error(`Invalid API response schema - ${url} :`, JSON.stringify(parsedData.error));
|
|
||||||
}
|
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
const parsedData = responseSchema.safeParse(data);
|
||||||
|
|
||||||
|
if (!parsedData.success) {
|
||||||
|
/** The request was successful, but the response data does not match the expected schema.
|
||||||
|
* This is a client side error, and should be fixed by updating the responseSchema to keep things typed.
|
||||||
|
* This error should not be shown to the user , it is for dev only.
|
||||||
|
*/
|
||||||
|
console.error(`Invalid API response schema - ${url} :`, JSON.stringify(parsedData.error));
|
||||||
|
}
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
return data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Request failed:", JSON.stringify(error));
|
console.error("Request failed:", JSON.stringify(error));
|
||||||
throw error;
|
throw error;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue