UNPKG

n8n-nodes-mautic-advanced

Version:

Enhanced n8n node for Mautic with comprehensive API coverage including tags, campaigns, categories, and advanced contact management

79 lines (78 loc) 4.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatNoResultsFound = exports.formatNotFoundError = exports.formatMissingIdError = exports.formatApiError = exports.wrapError = exports.wrapSuccess = exports.ERROR_TYPES = void 0; // nodes/MauticAdvanced/ai-tools/error-formatter.ts const authErrorHelpers_1 = require("../utils/authErrorHelpers"); exports.ERROR_TYPES = { API_ERROR: 'API_ERROR', ENTITY_NOT_FOUND: 'ENTITY_NOT_FOUND', NO_RESULTS_FOUND: 'NO_RESULTS_FOUND', MISSING_REQUIRED_FIELD: 'MISSING_REQUIRED_FIELD', MISSING_ENTITY_ID: 'MISSING_ENTITY_ID', INVALID_OPERATION: 'INVALID_OPERATION', WRITE_OPERATION_BLOCKED: 'WRITE_OPERATION_BLOCKED', PERMISSION_DENIED: 'PERMISSION_DENIED', VALIDATION_ERROR: 'VALIDATION_ERROR', }; function wrapSuccess(resource, operation, result) { return { schemaVersion: '1', success: true, operation, resource, result }; } exports.wrapSuccess = wrapSuccess; function wrapError(resource, operation, errorType, message, nextAction, context) { return { schemaVersion: '1', success: false, operation, resource, error: { errorType, message, nextAction, ...(context ? { context } : {}) }, }; } exports.wrapError = wrapError; function getErrorMessage(error) { if (error instanceof Error) { return error.message; } if (typeof error === 'string') { return error; } return String(error); } function formatApiError(error, resource, operation) { const message = getErrorMessage(error); const lower = message.toLowerCase(); if ((0, authErrorHelpers_1.isInvalidGrantError)(error)) { return wrapError(resource, operation, exports.ERROR_TYPES.PERMISSION_DENIED, authErrorHelpers_1.OAUTH_REFRESH_ERROR_MESSAGE, 'Retry the workflow; if it persists, reconnect the Mautic OAuth2 credential.'); } if (lower.includes('forbidden') || lower.includes('unauthor') || lower.includes('permission') || lower.includes('401') || lower.includes('403')) { return wrapError(resource, operation, exports.ERROR_TYPES.PERMISSION_DENIED, message, 'Verify API credentials and permissions, then retry.'); } if (lower.includes('not found') || lower.includes('does not exist') || lower.includes('404')) { return wrapError(resource, operation, exports.ERROR_TYPES.ENTITY_NOT_FOUND, message, `Call mauticadvanced_${resource} with operation 'getAll' and the 'search' parameter to find the record by name or text, extract its numeric 'id', then retry.`); } if (lower.includes('required') || lower.includes('missing') || lower.includes('blank')) { return wrapError(resource, operation, exports.ERROR_TYPES.MISSING_REQUIRED_FIELD, message, 'Check required fields for this resource and retry with all required parameters.'); } if (lower.includes('validation') || lower.includes('invalid') || lower.includes('unprocessable')) { return wrapError(resource, operation, exports.ERROR_TYPES.VALIDATION_ERROR, message, 'Check the field values and types, then retry with corrected parameters.'); } return wrapError(resource, operation, exports.ERROR_TYPES.API_ERROR, message, 'Verify parameter names and values, then retry.'); } exports.formatApiError = formatApiError; function formatMissingIdError(resource, operation) { return wrapError(resource, operation, exports.ERROR_TYPES.MISSING_ENTITY_ID, `A numeric entity ID is required for ${resource}.${operation}.`, `Provide a numeric 'id' parameter. If you only have a name or text, call mauticadvanced_${resource} with operation 'getAll' and the 'search' parameter to find the record and get its numeric 'id' first.`); } exports.formatMissingIdError = formatMissingIdError; function formatNotFoundError(resource, operation, id) { return wrapError(resource, operation, exports.ERROR_TYPES.ENTITY_NOT_FOUND, `No ${resource} record found with ID ${id}.`, `Call mauticadvanced_${resource} with operation 'getAll' and the 'search' parameter to find the record by text, then use the numeric ID from the results.`); } exports.formatNotFoundError = formatNotFoundError; function formatNoResultsFound(resource, operation, filters) { return wrapError(resource, operation, exports.ERROR_TYPES.NO_RESULTS_FOUND, `No ${resource} records matched the provided filters.`, 'Broaden your search criteria, check for typos, or verify the record exists.', { filtersUsed: filters }); } exports.formatNoResultsFound = formatNoResultsFound;