From 6a5cde6f40a9abffd879c59207c304a925402da0 Mon Sep 17 00:00:00 2001 From: Rim Date: Thu, 17 Apr 2025 06:43:54 -0400 Subject: [PATCH] refactor(serverUtils.js): better error handling --- src/js/serverUtils.js | 142 ++++++++++++++++++++++-------------------- 1 file changed, 73 insertions(+), 69 deletions(-) diff --git a/src/js/serverUtils.js b/src/js/serverUtils.js index 74a9342..cf03f43 100644 --- a/src/js/serverUtils.js +++ b/src/js/serverUtils.js @@ -154,10 +154,13 @@ 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); - }); - }; + return new Promise((_, reject) => { + setTimeout( + () => reject(new ApiTimeoutError(`Request timed out after ${ms}ms`)), + ms + ); + }); +}; // Helper function to ensure login const ensureLogin = async (ssoToken) => { @@ -179,80 +182,81 @@ 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); - this.name = 'ApiTimeoutError'; - } + constructor(message = 'API request timed out') { + super(message); + this.name = 'ApiTimeoutError'; } - - class ApiAuthError extends Error { - constructor(message = 'Authentication failed') { - super(message); - this.name = 'ApiAuthError'; - } +} + +class ApiAuthError extends Error { + constructor(message = 'Authentication failed') { + super(message); + this.name = 'ApiAuthError'; } - - class ApiDataError extends Error { - constructor(message = 'Data processing error') { - super(message); - this.name = 'ApiDataError'; - } +} + +class ApiDataError extends Error { + constructor(message = 'Data processing error') { + super(message); + this.name = 'ApiDataError'; } - - // Helper function to handle API errors - const handleApiError = (error, res) => { - logger.error('API Error:', error); - logger.error(`Error Stack: ${error.stack}`); - logger.error(`Error Time: ${global.Utils.toIsoString(new Date())}`); - - // Map error types to appropriate responses - const errorResponses = { - 'ApiTimeoutError': { - status: 200, - body: { - status: 'error', - message: 'The request timed out. Please try again.', - error_type: 'timeout', - timestamp: global.Utils.toIsoString(new Date()) - } +} + +// Helper function to handle API errors +const handleApiError = (error, res) => { + logger.error('API Error:', error); + logger.error(`Error Stack: ${error.stack}`); + logger.error(`Error Time: ${global.Utils.toIsoString(new Date())}`); + + // Map error types to appropriate responses + const errorResponses = { + ApiTimeoutError: { + status: 200, + body: { + status: 'error', + message: 'The request timed out. Please try again.', + error_type: 'timeout', + timestamp: global.Utils.toIsoString(new Date()), }, - 'ApiAuthError': { - status: 200, - body: { - status: 'error', - message: 'Authentication failed. Please check your SSO token.', - error_type: 'auth_failure', - timestamp: global.Utils.toIsoString(new Date()) - } + }, + ApiAuthError: { + status: 200, + body: { + status: 'error', + message: 'Authentication failed. Please check your SSO token.', + error_type: 'auth_failure', + timestamp: global.Utils.toIsoString(new Date()), }, - 'SyntaxError': { - status: 200, - body: { - status: 'error', - 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()) - } + }, + SyntaxError: { + status: 200, + body: { + status: 'error', + 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()), }, - 'default': { - status: 200, - body: { - status: 'error', - message: error.message || 'An unknown error occurred', - error_type: error.name || 'UnknownError', - timestamp: global.Utils.toIsoString(new Date()) - } - } - }; - - // Get the appropriate response or use default - const response = errorResponses[error.name] || errorResponses.default; - - return res.status(response.status).json(response.body); + }, + default: { + status: 200, + body: { + status: 'error', + message: error.message || 'An unknown error occurred', + error_type: error.name || 'UnknownError', + timestamp: global.Utils.toIsoString(new Date()), + }, + }, }; + // Get the appropriate response or use default + const response = errorResponses[error.name] || errorResponses.default; + + return res.status(response.status).json(response.body); +}; + // Helper function to remove sensitive data from headers function sanitizeHeaders(headers) { const safeHeaders = { ...headers };