|
| 1 | +import { InternalError } from '@lokalise/node-core' |
| 2 | +import { MessageInvalidFormatError, MessageValidationError } from '@message-queue-toolkit/core' |
| 3 | +import { describe, expect, it } from 'vitest' |
| 4 | +import { ZodError } from 'zod/v4' |
| 5 | +import { AmqpConsumerErrorResolver } from './AmqpConsumerErrorResolver.ts' |
| 6 | + |
| 7 | +describe('AmqpConsumerErrorResolver', () => { |
| 8 | + const resolver = new AmqpConsumerErrorResolver() |
| 9 | + |
| 10 | + it('returns MessageInvalidFormatError for SyntaxError', () => { |
| 11 | + const syntaxError = new SyntaxError('Unexpected token') |
| 12 | + |
| 13 | + const result = resolver.processError(syntaxError) |
| 14 | + |
| 15 | + expect(result).toBeInstanceOf(MessageInvalidFormatError) |
| 16 | + expect(result.message).toBe('Unexpected token') |
| 17 | + }) |
| 18 | + |
| 19 | + it('returns MessageValidationError for ZodError', () => { |
| 20 | + const zodError = new ZodError([ |
| 21 | + { |
| 22 | + code: 'invalid_type', |
| 23 | + expected: 'string', |
| 24 | + message: 'Expected string, received number', |
| 25 | + path: ['id'], |
| 26 | + input: 123, |
| 27 | + }, |
| 28 | + ]) |
| 29 | + |
| 30 | + const result = resolver.processError(zodError) |
| 31 | + |
| 32 | + expect(result).toBeInstanceOf(MessageValidationError) |
| 33 | + expect(result.details).toEqual({ |
| 34 | + error: zodError.issues, |
| 35 | + }) |
| 36 | + }) |
| 37 | + |
| 38 | + it('returns InternalError for standardized errors', () => { |
| 39 | + // Create an error that matches the StandardizedError interface |
| 40 | + // StandardizedError requires: name, message, code, and Symbol.for('StandardizedErrorSymbol') = true |
| 41 | + const standardizedError = Object.assign(new Error('Custom error message'), { |
| 42 | + code: 'CUSTOM_ERROR', |
| 43 | + [Symbol.for('StandardizedErrorSymbol')]: true, |
| 44 | + }) |
| 45 | + |
| 46 | + const result = resolver.processError(standardizedError) |
| 47 | + |
| 48 | + expect(result).toBeInstanceOf(InternalError) |
| 49 | + expect(result.message).toBe('Custom error message') |
| 50 | + expect(result.errorCode).toBe('CUSTOM_ERROR') |
| 51 | + }) |
| 52 | + |
| 53 | + it('returns InternalError with default message for unknown error types', () => { |
| 54 | + const unknownError = { someProperty: 'someValue' } |
| 55 | + |
| 56 | + const result = resolver.processError(unknownError) |
| 57 | + |
| 58 | + expect(result).toBeInstanceOf(InternalError) |
| 59 | + expect(result.message).toBe('Error processing message') |
| 60 | + expect(result.errorCode).toBe('INTERNAL_ERROR') |
| 61 | + }) |
| 62 | + |
| 63 | + it('returns InternalError with default message for null error', () => { |
| 64 | + const result = resolver.processError(null) |
| 65 | + |
| 66 | + expect(result).toBeInstanceOf(InternalError) |
| 67 | + expect(result.message).toBe('Error processing message') |
| 68 | + expect(result.errorCode).toBe('INTERNAL_ERROR') |
| 69 | + }) |
| 70 | + |
| 71 | + it('returns InternalError with default message for undefined error', () => { |
| 72 | + const result = resolver.processError(undefined) |
| 73 | + |
| 74 | + expect(result).toBeInstanceOf(InternalError) |
| 75 | + expect(result.message).toBe('Error processing message') |
| 76 | + expect(result.errorCode).toBe('INTERNAL_ERROR') |
| 77 | + }) |
| 78 | + |
| 79 | + it('returns InternalError with default message for string error', () => { |
| 80 | + const result = resolver.processError('some string error') |
| 81 | + |
| 82 | + expect(result).toBeInstanceOf(InternalError) |
| 83 | + expect(result.message).toBe('Error processing message') |
| 84 | + expect(result.errorCode).toBe('INTERNAL_ERROR') |
| 85 | + }) |
| 86 | +}) |
0 commit comments