n8n-nodes-netbox
Version:
n8n community node for NetBox API integration with comprehensive DCIM, IPAM, Virtualization, Circuits, Wireless, and data center management operations
89 lines (88 loc) • 4.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleApiError = handleApiError;
// helpers/errorHandler.ts
/**
* Handle API errors from NetBox
*/
function handleApiError(error, endpoint) {
console.log('Error in API request to endpoint:', endpoint);
console.log('Error details:', error);
// Handle HTTP response errors
if (error.response) {
const status = error.response.status || error.status;
const statusText = error.response.statusText || error.statusText || 'Unknown Error';
// Get error data from various possible locations
const errorData = error.response.data || error.response.body || error.error || {};
// LOG THE FULL ERROR RESPONSE FOR DEBUGGING
console.log('Full error response data:', JSON.stringify(errorData, null, 2));
// Handle specific NetBox API error formats
if (errorData.detail) {
return new Error(`NetBox API error (${status}): ${errorData.detail}`);
}
// Handle non_field_errors (common in NetBox validation errors)
if (errorData.non_field_errors && Array.isArray(errorData.non_field_errors)) {
return new Error(`NetBox API validation error (${status}): ${errorData.non_field_errors.join(', ')}`);
}
// Handle field-specific validation errors
if (typeof errorData === 'object' && errorData !== null) {
const fieldErrors = [];
Object.keys(errorData).forEach((field) => {
if (Array.isArray(errorData[field])) {
fieldErrors.push(`${field}: ${errorData[field].join(', ')}`);
}
else if (typeof errorData[field] === 'string') {
fieldErrors.push(`${field}: ${errorData[field]}`);
}
else if (typeof errorData[field] === 'object') {
// Handle nested validation errors
fieldErrors.push(`${field}: ${JSON.stringify(errorData[field])}`);
}
});
if (fieldErrors.length > 0) {
return new Error(`NetBox API validation error (${status}): ${fieldErrors.join('; ')}`);
}
// Fall back to JSON stringify for complex objects
try {
const errorMessage = JSON.stringify(errorData);
return new Error(`NetBox API error (${status}) on ${endpoint}: ${errorMessage}`);
}
catch (e) {
// If JSON stringification fails
return new Error(`NetBox API error (${status}) on ${endpoint}: ${errorData}`);
}
}
// Generic HTTP error
return new Error(`NetBox API error (${status} ${statusText}) on ${endpoint}`);
}
// Handle network and connection errors
if (error.code === 'ECONNREFUSED') {
return new Error(`Connection refused to NetBox API. Please check the URL and network connectivity.`);
}
if (error.code === 'ENOTFOUND') {
return new Error(`NetBox host not found. Please check the URL in your credentials configuration.`);
}
// Handle SSL/TLS errors
if (error.code === 'UNABLE_TO_VERIFY_LEAF_SIGNATURE' ||
error.code === 'DEPTH_ZERO_SELF_SIGNED_CERT' ||
error.code === 'CERT_HAS_EXPIRED' ||
error.code === 'SELF_SIGNED_CERT_IN_CHAIN' ||
error.code === 'CERT_UNTRUSTED' ||
(error.message && error.message.includes('certificate'))) {
return new Error(`SSL Certificate validation failed (${error.code || 'SSL_ERROR'}). Please disable "Verify SSL Certificate" in your NetBox API credentials to bypass SSL verification for self-signed certificates.`);
}
// Handle timeout errors
if (error.code === 'ETIMEDOUT' || error.code === 'ESOCKETTIMEDOUT') {
return new Error(`Request timeout. The NetBox API took too long to respond.`);
}
// For authentication errors (should be caught by status codes above, but just in case)
if (error.message && error.message.includes('401')) {
return new Error(`Authentication failed. Please check your NetBox API token.`);
}
// For any other structured errors
if (error.message) {
return new Error(`NetBox API error on ${endpoint}: ${error.message}`);
}
// For non-structured errors or network issues
return new Error(`NetBox API error on ${endpoint}: ${error}`);
}