UNPKG

@gp_jcisneros/errors

Version:

Error handling utilities for GreenPay microservices and validation middleware

158 lines (133 loc) 6.21 kB
const { AWSError } = require('../src/AWSError'); describe('AWSError', () => { test('should create a DynamoDB error', () => { const error = AWSError.dynamoDB('ConditionalCheckFailedException', 'Condition check failed'); expect(error.message).toBe('Condition check failed'); expect(error.statusCode).toBe(500); expect(error.name).toBe('AWSError'); expect(error.type).toBe('AWS_ERROR'); expect(error.service).toBe('DYNAMODB'); expect(error.awsErrorCode).toBe('ConditionalCheckFailedException'); expect(error.errorCode).toBe('AWS_DYNAMODB'); expect(error.description).toBe('Condition check failed'); expect(error.integration).toBe('aws-service'); }); test('should create an S3 error', () => { const error = AWSError.s3('NoSuchKey', 'Object not found'); expect(error.message).toBe('Object not found'); expect(error.service).toBe('S3'); expect(error.awsErrorCode).toBe('NoSuchKey'); expect(error.errorCode).toBe('AWS_S3'); expect(error.description).toBe('Object not found'); expect(error.integration).toBe('aws-service'); expect(error.statusCode).toBe(500); }); test('should create a Lambda error', () => { const error = AWSError.lambda('ResourceNotFoundException', 'Function not found'); expect(error.message).toBe('Function not found'); expect(error.service).toBe('LAMBDA'); expect(error.awsErrorCode).toBe('ResourceNotFoundException'); expect(error.errorCode).toBe('AWS_LAMBDA'); expect(error.description).toBe('Function not found'); expect(error.integration).toBe('aws-service'); expect(error.statusCode).toBe(500); }); test('should create an SQS error', () => { const error = AWSError.sqs('QueueDoesNotExist', 'Queue not found'); expect(error.message).toBe('Queue not found'); expect(error.service).toBe('SQS'); expect(error.awsErrorCode).toBe('QueueDoesNotExist'); expect(error.errorCode).toBe('AWS_SQS'); expect(error.description).toBe('Queue not found'); expect(error.integration).toBe('aws-service'); expect(error.statusCode).toBe(500); }); test('should create a custom AWS service error', () => { const error = AWSError.forService('EC2', 'InvalidInstanceID.NotFound', 'Instance not found'); expect(error.message).toBe('Instance not found'); expect(error.service).toBe('EC2'); expect(error.awsErrorCode).toBe('InvalidInstanceID.NotFound'); expect(error.errorCode).toBe('AWS_EC2'); expect(error.description).toBe('Instance not found'); expect(error.integration).toBe('aws-service'); expect(error.statusCode).toBe(500); }); test('should create error without specific service', () => { const error = new AWSError('General AWS error'); expect(error.message).toBe('General AWS error'); expect(error.service).toBe(null); expect(error.awsErrorCode).toBe(null); expect(error.errorCode).toBe('AWS_GENERAL'); expect(error.description).toBe('General AWS error'); expect(error.integration).toBe('aws-service'); expect(error.statusCode).toBe(500); }); test('should create a credentials error', () => { const error = AWSError.credentials('Invalid credentials'); expect(error.message).toBe('Invalid credentials'); expect(error.service).toBe('CREDENTIALS'); expect(error.awsErrorCode).toBe('AUTH'); expect(error.errorCode).toBe('AWS_CREDENTIALS'); expect(error.description).toBe('Invalid credentials'); expect(error.integration).toBe('aws-service'); expect(error.statusCode).toBe(500); }); test('should create a region error', () => { const error = AWSError.region('Invalid region'); expect(error.message).toBe('Invalid region'); expect(error.service).toBe('REGION'); expect(error.awsErrorCode).toBe('CONFIG'); expect(error.errorCode).toBe('AWS_REGION'); expect(error.description).toBe('Invalid region'); expect(error.integration).toBe('aws-service'); expect(error.statusCode).toBe(500); }); test('should create a timeout error', () => { const error = AWSError.timeout('DYNAMODB', 'GET', 30); expect(error.message).toBe('DYNAMODB GET operation timed out after 30s'); expect(error.service).toBe('DYNAMODB'); expect(error.awsErrorCode).toBe('TIMEOUT'); expect(error.errorCode).toBe('AWS_DYNAMODB'); expect(error.description).toBe('DYNAMODB GET operation timed out after 30s'); expect(error.integration).toBe('aws-service'); expect(error.statusCode).toBe(500); }); test('should get AWS details', () => { const error = AWSError.dynamoDB('ConditionalCheckFailedException', 'Condition check failed'); const details = error.getAWSDetails(); expect(details.service).toBe('DYNAMODB'); expect(details.awsErrorCode).toBe('ConditionalCheckFailedException'); expect(details.message).toBe('Condition check failed'); expect(details.type).toBe('AWS_ERROR'); expect(details.errorCode).toBe('AWS_DYNAMODB'); expect(details.description).toBe('Condition check failed'); expect(details.integration).toBe('aws-service'); }); test('should have required fields', () => { const error = AWSError.dynamoDB('ConditionalCheckFailedException', 'Condition check failed'); expect(error.hasRequiredFields()).toBe(true); expect(error.getRequiredFields()).toEqual({ errorCode: 'AWS_DYNAMODB', description: 'Condition check failed', integration: 'aws-service' }); }); test('should convert to JSON', () => { const error = AWSError.s3('NoSuchKey', 'Object not found'); const json = error.toJSON(); expect(json.name).toBe('AWSError'); expect(json.message).toBe('Object not found'); expect(json.statusCode).toBe(500); expect(json.errorCode).toBe('AWS_S3'); expect(json.description).toBe('Object not found'); expect(json.integration).toBe('aws-service'); expect(json.timestamp).toBeDefined(); expect(json.stack).toBeDefined(); }); test('should handle null service and error code', () => { const error = new AWSError('Test error', null, null); expect(error.service).toBe(null); expect(error.awsErrorCode).toBe(null); expect(error.errorCode).toBe('AWS_GENERAL'); }); });