refactor(serverUtils.js): better error handling
This commit is contained in:
parent
3774a4265c
commit
6a5cde6f40
@ -155,9 +155,12 @@ const activeSessions = new Map();
|
|||||||
// Utility function to create a timeout promise
|
// Utility function to create a timeout promise
|
||||||
const timeoutPromise = (ms) => {
|
const timeoutPromise = (ms) => {
|
||||||
return new Promise((_, reject) => {
|
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
|
||||||
|
);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// Helper function to ensure login
|
// Helper function to ensure login
|
||||||
const ensureLogin = async (ssoToken) => {
|
const ensureLogin = async (ssoToken) => {
|
||||||
@ -179,79 +182,80 @@ const ensureLogin = async (ssoToken) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Helper function to handle API errors
|
// Create custom error classes
|
||||||
class ApiTimeoutError extends Error {
|
class ApiTimeoutError extends Error {
|
||||||
constructor(message = 'API request timed out') {
|
constructor(message = 'API request timed out') {
|
||||||
super(message);
|
super(message);
|
||||||
this.name = 'ApiTimeoutError';
|
this.name = 'ApiTimeoutError';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ApiAuthError extends Error {
|
class ApiAuthError extends Error {
|
||||||
constructor(message = 'Authentication failed') {
|
constructor(message = 'Authentication failed') {
|
||||||
super(message);
|
super(message);
|
||||||
this.name = 'ApiAuthError';
|
this.name = 'ApiAuthError';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ApiDataError extends Error {
|
class ApiDataError extends Error {
|
||||||
constructor(message = 'Data processing error') {
|
constructor(message = 'Data processing error') {
|
||||||
super(message);
|
super(message);
|
||||||
this.name = 'ApiDataError';
|
this.name = 'ApiDataError';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper function to handle API errors
|
// Helper function to handle API errors
|
||||||
const handleApiError = (error, res) => {
|
const handleApiError = (error, res) => {
|
||||||
logger.error('API Error:', error);
|
logger.error('API Error:', error);
|
||||||
logger.error(`Error Stack: ${error.stack}`);
|
logger.error(`Error Stack: ${error.stack}`);
|
||||||
logger.error(`Error Time: ${global.Utils.toIsoString(new Date())}`);
|
logger.error(`Error Time: ${global.Utils.toIsoString(new Date())}`);
|
||||||
|
|
||||||
// Map error types to appropriate responses
|
// Map error types to appropriate responses
|
||||||
const errorResponses = {
|
const errorResponses = {
|
||||||
'ApiTimeoutError': {
|
ApiTimeoutError: {
|
||||||
status: 200,
|
status: 200,
|
||||||
body: {
|
body: {
|
||||||
status: 'error',
|
status: 'error',
|
||||||
message: 'The request timed out. Please try again.',
|
message: 'The request timed out. Please try again.',
|
||||||
error_type: 'timeout',
|
error_type: 'timeout',
|
||||||
timestamp: global.Utils.toIsoString(new Date())
|
timestamp: global.Utils.toIsoString(new Date()),
|
||||||
}
|
|
||||||
},
|
},
|
||||||
'ApiAuthError': {
|
},
|
||||||
|
ApiAuthError: {
|
||||||
status: 200,
|
status: 200,
|
||||||
body: {
|
body: {
|
||||||
status: 'error',
|
status: 'error',
|
||||||
message: 'Authentication failed. Please check your SSO token.',
|
message: 'Authentication failed. Please check your SSO token.',
|
||||||
error_type: 'auth_failure',
|
error_type: 'auth_failure',
|
||||||
timestamp: global.Utils.toIsoString(new Date())
|
timestamp: global.Utils.toIsoString(new Date()),
|
||||||
}
|
|
||||||
},
|
},
|
||||||
'SyntaxError': {
|
},
|
||||||
|
SyntaxError: {
|
||||||
status: 200,
|
status: 200,
|
||||||
body: {
|
body: {
|
||||||
status: 'error',
|
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',
|
error_type: 'InvalidResponseError',
|
||||||
timestamp: global.Utils.toIsoString(new Date())
|
timestamp: global.Utils.toIsoString(new Date()),
|
||||||
}
|
|
||||||
},
|
},
|
||||||
'default': {
|
},
|
||||||
|
default: {
|
||||||
status: 200,
|
status: 200,
|
||||||
body: {
|
body: {
|
||||||
status: 'error',
|
status: 'error',
|
||||||
message: error.message || 'An unknown error occurred',
|
message: error.message || 'An unknown error occurred',
|
||||||
error_type: error.name || 'UnknownError',
|
error_type: error.name || 'UnknownError',
|
||||||
timestamp: global.Utils.toIsoString(new Date())
|
timestamp: global.Utils.toIsoString(new Date()),
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
// Get the appropriate response or use default
|
// Get the appropriate response or use default
|
||||||
const response = errorResponses[error.name] || errorResponses.default;
|
const response = errorResponses[error.name] || errorResponses.default;
|
||||||
|
|
||||||
return res.status(response.status).json(response.body);
|
return res.status(response.status).json(response.body);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Helper function to remove sensitive data from headers
|
// Helper function to remove sensitive data from headers
|
||||||
function sanitizeHeaders(headers) {
|
function sanitizeHeaders(headers) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user