@panoptic-it-solutions/n8n-nodes-datto-rmm
Version:
n8n node for Datto RMM integration
198 lines • 8.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleErrors = handleErrors;
exports.validateRequiredParams = validateRequiredParams;
exports.validateParamTypes = validateParamTypes;
const n8n_workflow_1 = require("n8n-workflow");
async function handleErrors(context, operation) {
try {
return await operation();
}
catch (error) {
const dattoError = error;
if ('httpCode' in dattoError && dattoError.httpCode) {
return handleHttpError(context, dattoError);
}
if (('isAxiosError' in dattoError && dattoError.isAxiosError) ||
('response' in dattoError && dattoError.response)) {
return handleApiError(context, dattoError);
}
return handleGenericError(context, dattoError);
}
}
function handleHttpError(context, error) {
var _a, _b;
const statusCode = error.httpCode || ((_a = error.response) === null || _a === void 0 ? void 0 : _a.status);
const responseData = ((_b = error.response) === null || _b === void 0 ? void 0 : _b.data) || error.responseData || {};
let message;
switch (statusCode) {
case 400:
message = `Bad Request: ${responseData.message || 'Invalid parameters provided'}`;
break;
case 401:
message = 'Authentication failed. Please check your API credentials';
break;
case 403:
message = 'Access forbidden. You do not have permission to perform this operation';
break;
case 404:
message = `Resource not found: ${responseData.message || 'The requested resource does not exist'}`;
break;
case 409:
message = `Conflict: ${responseData.message || 'Resource already exists or conflicts with existing data'}`;
break;
case 429:
message = 'Rate limit exceeded. Please wait before making more requests';
break;
case 500:
message = 'Internal server error. Please try again later';
break;
case 502:
case 503:
case 504:
message = 'Datto RMM service is temporarily unavailable. Please try again later';
break;
default:
message = `HTTP ${statusCode}: ${responseData.message || error.message || 'An unexpected error occurred'}`;
}
throw new n8n_workflow_1.NodeApiError(context.getNode(), error, {
message,
description: responseData.description || 'Check your request parameters and try again',
httpCode: (statusCode === null || statusCode === void 0 ? void 0 : statusCode.toString()) || '500',
});
}
function handleApiError(context, error) {
var _a;
const response = error.response;
const responseData = (response === null || response === void 0 ? void 0 : response.data) || {};
const errorMessage = responseData.error ||
responseData.message ||
responseData.error_description ||
error.message ||
'API request failed';
const details = responseData.error_details ||
responseData.details ||
responseData.description ||
'No additional details available';
throw new n8n_workflow_1.NodeApiError(context.getNode(), error, {
message: `Datto RMM API Error: ${errorMessage}`,
description: details,
httpCode: ((_a = response === null || response === void 0 ? void 0 : response.status) === null || _a === void 0 ? void 0 : _a.toString()) || '500',
});
}
function handleGenericError(context, error) {
var _a, _b;
if (error instanceof n8n_workflow_1.NodeApiError || error instanceof n8n_workflow_1.NodeOperationError) {
throw error;
}
if (error.name === 'ValidationError' || error.code === 'VALIDATION_ERROR') {
throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Validation Error: ${error.message || 'Invalid input'}`, {
description: 'Please check your input parameters and try again',
});
}
if (error.code === 'ETIMEDOUT' || ((_a = error.message) === null || _a === void 0 ? void 0 : _a.includes('timeout'))) {
throw new n8n_workflow_1.NodeApiError(context.getNode(), error, {
message: 'Request timeout: Datto RMM API did not respond in time',
description: 'The request took too long to complete. Please try again',
});
}
if (error.code === 'ECONNREFUSED' || error.code === 'ENOTFOUND') {
throw new n8n_workflow_1.NodeApiError(context.getNode(), error, {
message: 'Network Error: Unable to connect to Datto RMM API',
description: 'Please check your network connection and API URL configuration',
});
}
if (((_b = error.message) === null || _b === void 0 ? void 0 : _b.includes('Invalid URL')) || error.code === 'ERR_INVALID_URL') {
throw new n8n_workflow_1.NodeOperationError(context.getNode(), 'Invalid API URL configuration', {
description: 'Please ensure your API URL is correctly formatted (e.g., https://pinotage-api.centrastage.net) without trailing slashes or /api suffix',
});
}
throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Unexpected error: ${error.message || 'An unknown error occurred'}`, {
description: 'Please check your configuration and try again. If the problem persists, contact support',
});
}
function validateRequiredParams(context, params, requiredFields) {
const missingFields = [];
for (const field of requiredFields) {
const value = params[field];
if (value === undefined || value === null || value === '') {
missingFields.push(field);
}
}
if (missingFields.length > 0) {
throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Missing required parameters: ${missingFields.join(', ')}`, {
description: 'Please provide all required parameters to continue',
});
}
}
function validateParamTypes(context, params, rules) {
const errors = [];
for (const [field, rule] of Object.entries(rules)) {
const value = params[field];
if (value === undefined || value === null) {
continue;
}
switch (rule.type) {
case 'string':
if (typeof value !== 'string') {
errors.push(`${field} must be a string`);
}
break;
case 'number':
if (typeof value !== 'number' || isNaN(value)) {
errors.push(`${field} must be a valid number`);
}
else {
if (rule.min !== undefined && value < rule.min) {
errors.push(`${field} must be at least ${rule.min}`);
}
if (rule.max !== undefined && value > rule.max) {
errors.push(`${field} must be at most ${rule.max}`);
}
}
break;
case 'boolean':
if (typeof value !== 'boolean') {
errors.push(`${field} must be a boolean`);
}
break;
case 'array':
if (!Array.isArray(value)) {
errors.push(`${field} must be an array`);
}
break;
}
if (rule.format && typeof value === 'string') {
switch (rule.format) {
case 'email': {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(value)) {
errors.push(`${field} must be a valid email address`);
}
break;
}
case 'url': {
try {
new URL(value);
}
catch {
errors.push(`${field} must be a valid URL`);
}
break;
}
case 'date': {
if (isNaN(Date.parse(value))) {
errors.push(`${field} must be a valid date`);
}
break;
}
}
}
}
if (errors.length > 0) {
throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Parameter validation failed: ${errors.join(', ')}`, {
description: 'Please correct the parameter values and try again',
});
}
}
//# sourceMappingURL=errorHandler.js.map