n8n-nodes-arubacentralnextgen
Version:
n8n community node for Aruba Central NextGen API integration with modern monitoring and management capabilities
71 lines (70 loc) • 3.31 kB
JavaScript
;
// helpers/errorHandler.ts
Object.defineProperty(exports, "__esModule", { value: true });
exports.ArubaCentralError = exports.ErrorCode = void 0;
exports.handleApiError = handleApiError;
exports.validateParameters = validateParameters;
const logger_1 = require("./logger");
var ErrorCode;
(function (ErrorCode) {
ErrorCode["AUTHENTICATION_FAILED"] = "AUTHENTICATION_FAILED";
ErrorCode["INVALID_PARAMETERS"] = "INVALID_PARAMETERS";
ErrorCode["API_ERROR"] = "API_ERROR";
ErrorCode["RATE_LIMIT_EXCEEDED"] = "RATE_LIMIT_EXCEEDED";
ErrorCode["NETWORK_ERROR"] = "NETWORK_ERROR";
ErrorCode["PAGINATION_ERROR"] = "PAGINATION_ERROR";
ErrorCode["HIERARCHY_ERROR"] = "HIERARCHY_ERROR";
ErrorCode["UNKNOWN_ERROR"] = "UNKNOWN_ERROR";
})(ErrorCode || (exports.ErrorCode = ErrorCode = {}));
class ArubaCentralError extends Error {
constructor(message, code, context, originalError) {
super(message);
this.name = 'ArubaCentralError';
this.code = code;
this.context = context;
this.originalError = originalError;
// Log the error when it's created
logger_1.logger.error(`${code}: ${message}`, this);
}
}
exports.ArubaCentralError = ArubaCentralError;
function handleApiError(error) {
var _a, _b;
if (error.response) {
// The request was made and the server responded with a status code outside of 2xx
const status = error.response.status;
const message = ((_a = error.response.body) === null || _a === void 0 ? void 0 : _a.description) || ((_b = error.response.body) === null || _b === void 0 ? void 0 : _b.message) || error.message;
const context = {
status,
body: error.response.body,
headers: error.response.headers,
};
// Check for specific error types based on status codes
if (status === 401 || status === 403) {
throw new ArubaCentralError(`Authentication failed: ${message}`, ErrorCode.AUTHENTICATION_FAILED, context, error);
}
else if (status === 400) {
throw new ArubaCentralError(`Invalid request parameters: ${message}`, ErrorCode.INVALID_PARAMETERS, context, error);
}
else if (status === 429) {
throw new ArubaCentralError(`Rate limit exceeded: ${message}`, ErrorCode.RATE_LIMIT_EXCEEDED, context, error);
}
else {
throw new ArubaCentralError(`API error (${status}): ${message}`, ErrorCode.API_ERROR, context, error);
}
}
else if (error.request) {
// The request was made but no response was received
throw new ArubaCentralError(`Network error: No response received from Aruba Central API`, ErrorCode.NETWORK_ERROR, { request: error.request }, error);
}
else {
// Something else caused the error
throw new ArubaCentralError(error.message || 'Unknown error occurred', ErrorCode.UNKNOWN_ERROR, {}, error);
}
}
function validateParameters(params, requiredParams) {
const missingParams = requiredParams.filter((param) => !params.hasOwnProperty(param));
if (missingParams.length > 0) {
throw new ArubaCentralError(`Missing required parameters: ${missingParams.join(', ')}`, ErrorCode.INVALID_PARAMETERS, { missingParams });
}
}