fix: improve error handling for unauthorized responses and response parsing in BaseApiService

This commit is contained in:
DESKTOP-RTLN3BA\$punk 2026-01-12 12:47:08 -08:00
parent 61f220b862
commit 7ac7cd5f99

View file

@ -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.",