refactor(serverUtils.js): better error handling
This commit is contained in:
parent
3774a4265c
commit
6a5cde6f40
@ -155,7 +155,10 @@ const activeSessions = new Map();
|
||||
// Utility function to create a timeout promise
|
||||
const timeoutPromise = (ms) => {
|
||||
return new Promise((_, reject) => {
|
||||
setTimeout(() => reject(new ApiTimeoutError(`Request timed out after ${ms}ms`)), ms);
|
||||
setTimeout(
|
||||
() => reject(new ApiTimeoutError(`Request timed out after ${ms}ms`)),
|
||||
ms
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
@ -179,7 +182,7 @@ const ensureLogin = async (ssoToken) => {
|
||||
}
|
||||
};
|
||||
|
||||
// Helper function to handle API errors
|
||||
// Create custom error classes
|
||||
class ApiTimeoutError extends Error {
|
||||
constructor(message = 'API request timed out') {
|
||||
super(message);
|
||||
@ -209,42 +212,43 @@ class ApiTimeoutError extends Error {
|
||||
|
||||
// Map error types to appropriate responses
|
||||
const errorResponses = {
|
||||
'ApiTimeoutError': {
|
||||
ApiTimeoutError: {
|
||||
status: 200,
|
||||
body: {
|
||||
status: 'error',
|
||||
message: 'The request timed out. Please try again.',
|
||||
error_type: 'timeout',
|
||||
timestamp: global.Utils.toIsoString(new Date())
|
||||
}
|
||||
timestamp: global.Utils.toIsoString(new Date()),
|
||||
},
|
||||
'ApiAuthError': {
|
||||
},
|
||||
ApiAuthError: {
|
||||
status: 200,
|
||||
body: {
|
||||
status: 'error',
|
||||
message: 'Authentication failed. Please check your SSO token.',
|
||||
error_type: 'auth_failure',
|
||||
timestamp: global.Utils.toIsoString(new Date())
|
||||
}
|
||||
timestamp: global.Utils.toIsoString(new Date()),
|
||||
},
|
||||
'SyntaxError': {
|
||||
},
|
||||
SyntaxError: {
|
||||
status: 200,
|
||||
body: {
|
||||
status: 'error',
|
||||
message: 'Failed to parse API response. This usually means the SSO token is invalid or expired.',
|
||||
message:
|
||||
'Failed to parse API response. This usually means the SSO token is invalid or expired.',
|
||||
error_type: 'InvalidResponseError',
|
||||
timestamp: global.Utils.toIsoString(new Date())
|
||||
}
|
||||
timestamp: global.Utils.toIsoString(new Date()),
|
||||
},
|
||||
'default': {
|
||||
},
|
||||
default: {
|
||||
status: 200,
|
||||
body: {
|
||||
status: 'error',
|
||||
message: error.message || 'An unknown error occurred',
|
||||
error_type: error.name || 'UnknownError',
|
||||
timestamp: global.Utils.toIsoString(new Date())
|
||||
}
|
||||
}
|
||||
timestamp: global.Utils.toIsoString(new Date()),
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
// Get the appropriate response or use default
|
||||
|
Loading…
x
Reference in New Issue
Block a user