UNPKG

@gp_jcisneros/errors

Version:

Error handling utilities for GreenPay microservices

159 lines (144 loc) 4.69 kB
/** * Base custom error class for GreenPay microservices * Focused on standardized error handling with required fields */ class CustomError extends Error { /** * Create a new CustomError instance * @param {string} message - Error message * @param {Object} options - Error options * @param {string} options.errorCode - Código alfanumérico que identifica el error * @param {string} options.description - Descripción técnica del error (defaults to message) * @param {string} options.integration - Nombre del módulo donde se está usando * @param {number} options.statusCode - HTTP status code (defaults to 500) * @param {Object} options.additionalFields - Additional fields to add to the error */ constructor(message, options = {}) { super(message); this.name = 'CustomError'; this.timestamp = new Date().toISOString(); // Extract options with defaults const { errorCode = null, description = message, integration = null, statusCode = 500, ...additionalFields } = options; // Required fields for error standardization this.errorCode = errorCode; // Código alfanumérico que identifica el error this.description = description; // Descripción Tecnica del ERROR this.integration = integration; // Nombre del módulo donde se está usando this.statusCode = statusCode; // HTTP status code // Add any additional fields Object.assign(this, additionalFields); // Capture stack trace Error.captureStackTrace(this, this.constructor); } /** * Create a CustomError with required fields * @param {string} message - Error message * @param {string} errorCode - Error code * @param {string} description - Error description * @param {string} integration - Integration name * @param {number} statusCode - HTTP status code * @returns {CustomError} - New CustomError instance */ static create(message, errorCode, description, integration, statusCode = 500) { return new CustomError(message, { errorCode, description, integration, statusCode }); } /** * Create a CustomError with minimal required fields * @param {string} message - Error message * @param {string} errorCode - Error code * @param {string} integration - Integration name * @returns {CustomError} - New CustomError instance */ static createMinimal(message, errorCode, integration) { return new CustomError(message, { errorCode, integration }); } /** * Set error code (alfanumeric code that identifies the error) * @param {string} errorCode - Error code * @returns {CustomError} - This error instance */ setErrorCode(errorCode) { this.errorCode = errorCode; return this; } /** * Set description (error description) * @param {string} description - Error description * @returns {CustomError} - This error instance */ setDescription(description) { this.description = description; return this; } /** * Set integration (module name where the error is being used) * @param {string} integration - Integration/module name * @returns {CustomError} - This error instance */ setIntegration(integration) { this.integration = integration; return this; } /** * Set all required fields at once * @param {string} errorCode - Error code * @param {string} description - Error description * @param {string} integration - Integration/module name * @returns {CustomError} - This error instance */ setRequiredFields(errorCode, description, integration) { this.errorCode = errorCode; this.description = description; this.integration = integration; return this; } /** * Get required error fields * @returns {Object} - Required error fields */ getRequiredFields() { return { errorCode: this.errorCode, description: this.description, integration: this.integration, }; } /** * Check if error has all required fields * @returns {boolean} - True if all required fields are present */ hasRequiredFields() { return !!(this.errorCode && this.description && this.integration); } /** * Get error as JSON object * @returns {Object} - Error as JSON */ toJSON() { return { name: this.name, message: this.message, statusCode: this.statusCode, timestamp: this.timestamp, stack: this.stack, // Required fields errorCode: this.errorCode, description: this.description, integration: this.integration, }; } } module.exports = { CustomError };