Refactor error handling and logging in API responses
This commit is contained in:
@ -27,14 +27,17 @@ export function redirectToLogin() {
|
||||
/** @returns {boolean} true if the response means the session is no longer valid */
|
||||
function isSessionInvalidResponse(res, json) {
|
||||
if (res.status === 401) return true;
|
||||
const errMsg = json.error?.message || json.error || '';
|
||||
return res.status === 403 && (
|
||||
errMsg === 'Invalid or expired token'
|
||||
|| errMsg === 'Missing Bearer token'
|
||||
|| errMsg === 'Password change required before access'
|
||||
|| json.error?.code === 'UNAUTHORIZED'
|
||||
|| json.error?.code === 'PASSWORD_CHANGE_REQUIRED'
|
||||
);
|
||||
const errMsg = json.error?.message || (typeof json.error === 'string' ? json.error : '');
|
||||
const errCode = json.error?.code || '';
|
||||
return res.status === 401
|
||||
|| res.status === 403 && (
|
||||
errCode === 'UNAUTHORIZED'
|
||||
|| errCode === 'PASSWORD_CHANGE_REQUIRED'
|
||||
|| errCode === 'FORBIDDEN'
|
||||
|| errMsg === 'Invalid or expired token'
|
||||
|| errMsg === 'Missing Bearer token'
|
||||
|| errMsg === 'Password change required before access'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -125,7 +128,9 @@ function unwrap(json) {
|
||||
const d = json.data || {};
|
||||
return { success: true, ...(Array.isArray(d) ? { items: d } : d) };
|
||||
}
|
||||
return { success: false, error: json.error?.message || 'Unknown error' };
|
||||
const err = json.error;
|
||||
const msg = typeof err === 'string' ? err : (err?.message || 'Unknown error');
|
||||
return { success: false, error: msg, errorCode: typeof err === 'object' ? err?.code : null };
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user