UNPKG

@gp_jcisneros/errors

Version:

Error handling utilities for GreenPay microservices

104 lines (96 loc) 3.09 kB
const { CustomError } = require('./CustomError'); /** * Integration error class for external service errors */ class IntegrationError extends CustomError { constructor(message, integrationCode, integrationName, context = {}) { super(message, { ...context, // Set required fields errorCode: integrationCode || 'INTEGRATION_ERROR', description: message, integration: integrationName, statusCode: 502 }); this.name = 'IntegrationError'; this.type = 'INTEGRATION_ERROR'; this.integrationCode = integrationCode; this.integrationName = integrationName; this.context = context; } /** * Create a Cybersource error * @param {string} message - Error message * @param {string} code - Cybersource error code * @param {Object} context - Additional context * @returns {IntegrationError} - Cybersource error */ static cybersource(message, code, context = {}) { return new IntegrationError(message, code, 'cybersource', context); } /** * Create a Stripe error * @param {string} message - Error message * @param {string} code - Stripe error code * @param {Object} context - Additional context * @returns {IntegrationError} - Stripe error */ static stripe(message, code, context = {}) { return new IntegrationError(message, code, 'stripe', context); } /** * Create an Adyen error * @param {string} message - Error message * @param {string} code - Adyen error code * @param {Object} context - Additional context * @returns {IntegrationError} - Adyen error */ static adyen(message, code, context = {}) { return new IntegrationError(message, code, 'adyen', context); } /** * Create a custom integration error * @param {string} message - Error message * @param {string} code - Integration error code * @param {string} integrationName - Integration name * @param {Object} context - Additional context * @returns {IntegrationError} - Custom integration error */ static custom(message, code, integrationName, context = {}) { return new IntegrationError(message, code, integrationName, context); } /** * Get integration error details * @returns {Object} - Integration error details */ getIntegrationDetails() { return { integrationCode: this.integrationCode, integrationName: this.integrationName, message: this.message, type: this.type, // Required fields errorCode: this.errorCode, description: this.description, integration: this.integration, }; } /** * Convert to standard format with integration info * @returns {Object} - Standard format with integration details */ toStandardFormat() { return { code: this.statusCode, status: 'failed', message: this.message, error_code: this.integrationCode, // Required fields description: this.description, integration: this.integration, // Integration details error_details: this.context, }; } } module.exports = { IntegrationError };