@gp_jcisneros/errors
Version:
Error handling utilities for GreenPay microservices and validation middleware
194 lines (178 loc) • 5.84 kB
JavaScript
// eslint-disable-next-line no-unused-vars
const { ZodError: ZodValidationError } = require('zod');
const { CustomError } = require('./CustomError');
/**
* Zod error class for Zod validation failures
*/
class ZodError extends CustomError {
constructor(message, field = null, value = null, zodError = null) {
super(message, {
errorCode: `ZOD_${field ? field.toUpperCase() : 'GENERAL'}`,
description: message,
integration: 'zod-validation',
statusCode: 400
});
this.name = 'ZodError';
this.type = 'ZOD_ERROR';
this.field = field;
this.value = value;
this.zodError = zodError;
}
/**
* Create a Zod error from a ZodValidationError instance
* @param {ZodValidationError} zodError - Original Zod error
* @param {string} integration - Integration name
* @returns {ZodError} - Zod error
*/
static fromZodError(zodError, integration = 'zod-validation') {
const firstError = zodError.issues[0];
const field = firstError?.path?.join('.') || 'unknown';
const message = firstError?.message || 'Validation failed';
const error = new ZodError(
`Event object failed validation: ${message}`,
field,
firstError?.received,
zodError
);
error.setIntegration(integration);
error.setErrorCode('ZOD-001');
error.setDescription('Schema validation failed');
error.statusCode = 400;
// Add validation details as additional data
error.additionalData = {
validation: {
errors: zodError.issues.map((err) => ({
field: err.path.join('.'),
message: err.message,
received: err.received,
expected: err.expected,
})),
issues: zodError.issues,
},
};
return error;
}
/**
* Create a required field error
* @param {string} field - Field name
* @param {string} integration - Integration name
* @returns {ZodError} - Required field error
*/
static required(field, integration = 'zod-validation') {
const error = new ZodError(`${field} is required`, field, null);
error.setIntegration(integration);
error.setErrorCode('ZOD-002');
error.setDescription(`Campo '${field}' es requerido para validación dinámica`);
error.statusCode = 400;
return error;
}
/**
* Create an unsupported provider error
* @param {string} provider - Provider name
* @param {string} transactionType - Transaction type
* @param {string} integration - Integration name
* @returns {ZodError} - Unsupported provider error
*/
static unsupportedProvider(provider, transactionType, integration = 'zod-validation') {
const error = new ZodError(
`Proveedor '${provider}' no soportado para transacción tipo '${transactionType}'`,
'provider',
provider
);
error.setIntegration(integration);
error.setErrorCode('ZOD-003');
error.setDescription(`Proveedor '${provider}' no soportado para transacción tipo '${transactionType}'`);
error.statusCode = 400;
return error;
}
/**
* Create a generic validation error
* @param {Error} originalError - Original error
* @param {string} integration - Integration name
* @returns {ZodError} - Generic validation error
*/
static generic(originalError, integration = 'zod-validation') {
const error = new ZodError(
originalError.message || 'Internal validation error',
'general',
null,
originalError
);
error.setIntegration(integration);
error.setErrorCode('ZOD-004');
error.setDescription('Unexpected validation error');
error.statusCode = 500;
return error;
}
/**
* Create a schema validation error
* @param {string} message - Error message
* @param {string} field - Field name
* @param {any} value - Invalid value
* @param {string} integration - Integration name
* @returns {ZodError} - Schema validation error
*/
static schemaValidation(message, field = 'unknown', value = null, integration = 'zod-validation') {
const error = new ZodError(message, field, value);
error.setIntegration(integration);
error.setErrorCode('ZOD-005');
error.setDescription('Schema validation failed');
error.statusCode = 400;
return error;
}
/**
* Create a dynamic schema error
* @param {string} message - Error message
* @param {string} provider - Provider name
* @param {string} integration - Integration name
* @returns {ZodError} - Dynamic schema error
*/
static dynamicSchema(message, provider, integration = 'zod-validation') {
const error = new ZodError(message, 'dynamicSchema', provider);
error.setIntegration(integration);
error.setErrorCode('ZOD-006');
error.setDescription('Dynamic schema creation failed');
error.statusCode = 400;
return error;
}
/**
* Get Zod error details
* @returns {Object} - Zod error details
*/
getZodDetails() {
return {
field: this.field,
value: this.value,
message: this.message,
type: this.type,
zodError: this.zodError,
// Required fields
errorCode: this.errorCode,
description: this.description,
integration: this.integration,
};
}
/**
* Get validation errors from Zod error
* @returns {Array} - Array of validation errors
*/
getValidationErrors() {
if (!this.zodError || !this.zodError.issues) {
return [];
}
return this.zodError.issues.map((err) => ({
field: err.path.join('.'),
message: err.message,
received: err.received,
expected: err.expected,
}));
}
/**
* Check if error has Zod validation details
* @returns {boolean} - True if has Zod validation details
*/
hasZodDetails() {
return !!(this.zodError && this.zodError.issues);
}
}
module.exports = { ZodError };