UNPKG

n8n-nodes-arubacentral

Version:

n8n community node for Aruba Central API integration with comprehensive monitoring, configuration, and management capabilities

139 lines (138 loc) 5.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.handleApiError = handleApiError; exports.handleValidationError = handleValidationError; exports.handleApiErrorWithContinue = handleApiErrorWithContinue; // helpers/errorHandler.ts const n8n_workflow_1 = require("n8n-workflow"); const logger_1 = require("./logger"); /** * Handle API errors from Aruba Central * * @param this The n8n execution context * @param error The error that was caught * @param message Optional custom error message * @returns Never completes normally, always throws an error */ function handleApiError(error, message) { var _a; logger_1.logger.error('error:api', { originalMessage: error.message, customMessage: message }); // Special handling for rate limiting errors if (error.message && error.message.includes('rate limit')) { let retryAfter = 'unknown time'; // Try to extract the retry time from the error message const retryMatch = error.message.match(/(\d+) seconds/); if (retryMatch && retryMatch[1]) { retryAfter = `${retryMatch[1]} seconds`; } const rateMessage = `API rate limited. Please retry after ${retryAfter}.`; logger_1.logger.error('error:ratelimit', rateMessage); throw new n8n_workflow_1.NodeOperationError(this.getNode(), rateMessage); } if ((_a = error.response) === null || _a === void 0 ? void 0 : _a.body) { const errorBody = error.response.body; let errorMessage = message || 'Unknown error'; // Check for rate limiting in the response body if (typeof errorBody === 'object' && errorBody.message && errorBody.message.includes('rate limit')) { let retryAfter = 'unknown time'; // Try to extract the retry time from the message const retryMatch = errorBody.message.match(/(\d+) seconds/); if (retryMatch && retryMatch[1]) { retryAfter = `${retryMatch[1]} seconds`; } const rateMessage = `API rate limited. Please retry after ${retryAfter}.`; logger_1.logger.error('error:ratelimit', rateMessage); throw new n8n_workflow_1.NodeOperationError(this.getNode(), rateMessage); } // Process other types of error messages if (typeof errorBody === 'object') { if (errorBody.description) { errorMessage = errorBody.description; } else if (errorBody.error_description) { errorMessage = errorBody.error_description; } else if (errorBody.message) { errorMessage = errorBody.message; } else if (errorBody.detail) { errorMessage = errorBody.detail; } } else if (typeof errorBody === 'string') { try { const parsedBody = JSON.parse(errorBody); if (parsedBody.message) { errorMessage = parsedBody.message; } } catch (e) { // If parsing fails, use the string directly if it's not too long if (errorBody.length < 300) { errorMessage = errorBody; } } } logger_1.logger.error('error:api', `Throwing NodeApiError with message: ${errorMessage}`); throw new n8n_workflow_1.NodeApiError(this.getNode(), error, { message: errorMessage }); } if (message) { logger_1.logger.error('error:operation', `Throwing NodeOperationError with message: ${message}`); throw new n8n_workflow_1.NodeOperationError(this.getNode(), message); } logger_1.logger.error('error:unknown', 'Re-throwing original error'); throw error; } /** * Handle validation errors * * @param this The n8n execution context * @param message Error message describing the validation failure * @returns Never completes normally, always throws an error */ function handleValidationError(message) { logger_1.logger.error('error:validation', message); throw new n8n_workflow_1.NodeOperationError(this.getNode(), message); } /** * Handle API errors while allowing execution to continue * * @param this The n8n execution context * @param error The error that was caught * @param message Optional custom error message * @returns Array with a single item containing error details */ function handleApiErrorWithContinue(error, message) { var _a; logger_1.logger.error('error:api:continue', { originalMessage: error.message, customMessage: message }); // Special handling for rate limiting errors let errorMessage = message || error.message || 'Unknown error occurred'; // Check for rate limiting in the error message if (error.message && error.message.includes('rate limit')) { let retryAfter = 'unknown time'; // Try to extract the retry time from the error message const retryMatch = error.message.match(/(\d+) seconds/); if (retryMatch && retryMatch[1]) { retryAfter = `${retryMatch[1]} seconds`; } errorMessage = `API rate limited. Please retry after ${retryAfter}.`; logger_1.logger.error('error:ratelimit', errorMessage); } // If continueOnFail is enabled, return error as data if (this.continueOnFail()) { logger_1.logger.debug('error:api:continue', `Continuing execution with error: ${errorMessage}`); return [ { json: { success: false, error: errorMessage, details: ((_a = error.response) === null || _a === void 0 ? void 0 : _a.body) || {}, }, }, ]; } // Otherwise handle as regular error (will throw) return handleApiError.call(this, error, errorMessage); }