@gp_jcisneros/errors
Version:
Error handling utilities for GreenPay microservices
116 lines (98 loc) • 4.64 kB
JavaScript
const { IntegrationError } = require('../src/IntegrationError');
describe('IntegrationError', () => {
test('should create a cybersource error', () => {
const error = IntegrationError.cybersource('Card stolen', 'CYBERSOURCE_05');
expect(error.message).toBe('Card stolen');
expect(error.statusCode).toBe(502);
expect(error.name).toBe('IntegrationError');
expect(error.type).toBe('INTEGRATION_ERROR');
expect(error.integrationCode).toBe('CYBERSOURCE_05');
expect(error.integrationName).toBe('cybersource');
expect(error.errorCode).toBe('CYBERSOURCE_05');
expect(error.description).toBe('Card stolen');
expect(error.integration).toBe('cybersource');
});
test('should create a stripe error', () => {
const error = IntegrationError.stripe('Card declined', 'STRIPE_card_declined');
expect(error.message).toBe('Card declined');
expect(error.integrationCode).toBe('STRIPE_card_declined');
expect(error.integrationName).toBe('stripe');
expect(error.errorCode).toBe('STRIPE_card_declined');
expect(error.description).toBe('Card declined');
expect(error.integration).toBe('stripe');
expect(error.statusCode).toBe(502);
});
test('should create an adyen error', () => {
const error = IntegrationError.adyen('Payment failed', 'ADYEN_123');
expect(error.message).toBe('Payment failed');
expect(error.integrationCode).toBe('ADYEN_123');
expect(error.integrationName).toBe('adyen');
expect(error.errorCode).toBe('ADYEN_123');
expect(error.description).toBe('Payment failed');
expect(error.integration).toBe('adyen');
expect(error.statusCode).toBe(502);
});
test('should create a custom integration error', () => {
const error = IntegrationError.custom('Custom error', 'CUSTOM_001', 'custom-service');
expect(error.message).toBe('Custom error');
expect(error.integrationCode).toBe('CUSTOM_001');
expect(error.integrationName).toBe('custom-service');
expect(error.errorCode).toBe('CUSTOM_001');
expect(error.description).toBe('Custom error');
expect(error.integration).toBe('custom-service');
expect(error.statusCode).toBe(502);
});
test('should create error with context', () => {
const context = { transactionId: 'TXN_123', amount: 100 };
const error = IntegrationError.cybersource('Card stolen', 'CYBERSOURCE_05', context);
expect(error.message).toBe('Card stolen');
expect(error.integrationCode).toBe('CYBERSOURCE_05');
expect(error.integrationName).toBe('cybersource');
expect(error.errorCode).toBe('CYBERSOURCE_05');
expect(error.description).toBe('Card stolen');
expect(error.integration).toBe('cybersource');
expect(error.context).toEqual(context);
expect(error.statusCode).toBe(502);
});
test('should get integration details', () => {
const error = IntegrationError.cybersource('Card stolen', 'CYBERSOURCE_05');
const details = error.getIntegrationDetails();
expect(details.integrationCode).toBe('CYBERSOURCE_05');
expect(details.integrationName).toBe('cybersource');
expect(details.message).toBe('Card stolen');
expect(details.type).toBe('INTEGRATION_ERROR');
expect(details.errorCode).toBe('CYBERSOURCE_05');
expect(details.description).toBe('Card stolen');
expect(details.integration).toBe('cybersource');
});
test('should have required fields', () => {
const error = IntegrationError.cybersource('Card stolen', 'CYBERSOURCE_05');
expect(error.hasRequiredFields()).toBe(true);
expect(error.getRequiredFields()).toEqual({
errorCode: 'CYBERSOURCE_05',
description: 'Card stolen',
integration: 'cybersource'
});
});
test('should convert to JSON', () => {
const error = IntegrationError.cybersource('Card stolen', 'CYBERSOURCE_05');
const json = error.toJSON();
expect(json.name).toBe('IntegrationError');
expect(json.message).toBe('Card stolen');
expect(json.statusCode).toBe(502);
expect(json.errorCode).toBe('CYBERSOURCE_05');
expect(json.description).toBe('Card stolen');
expect(json.integration).toBe('cybersource');
expect(json.timestamp).toBeDefined();
expect(json.stack).toBeDefined();
});
test('should handle null integration code', () => {
const error = new IntegrationError('Test error', null, 'test-service');
expect(error.integrationCode).toBe(null);
expect(error.errorCode).toBe('INTEGRATION_ERROR');
});
test('should handle empty context', () => {
const error = new IntegrationError('Test error', 'TEST_001', 'test-service', {});
expect(error.context).toEqual({});
});
});