|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { HttpErrorCodes } from '../../../src/rest/constants.js'; |
| 3 | +import { ErrorHandlerRegistry } from '../../../src/rest/ErrorHandlerRegistry.js'; |
| 4 | +import type { HttpStatusCode } from '../../../src/types/rest.js'; |
| 5 | + |
| 6 | +const createErrorHandler = |
| 7 | + (statusCode: HttpStatusCode, message?: string) => (error: Error) => ({ |
| 8 | + statusCode, |
| 9 | + error: error.name, |
| 10 | + message: message ?? error.message, |
| 11 | + }); |
| 12 | + |
| 13 | +class CustomError extends Error { |
| 14 | + constructor(message: string) { |
| 15 | + super(message); |
| 16 | + this.name = 'CustomError'; |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +class AnotherError extends Error { |
| 21 | + constructor(message: string) { |
| 22 | + super(message); |
| 23 | + this.name = 'AnotherError'; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +class InheritedError extends CustomError { |
| 28 | + constructor(message: string) { |
| 29 | + super(message); |
| 30 | + this.name = 'InheritedError'; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +describe('Class: ErrorHandlerRegistry', () => { |
| 35 | + it('logs a warning when registering a duplicate error handler', () => { |
| 36 | + // Prepare |
| 37 | + const registry = new ErrorHandlerRegistry({ logger: console }); |
| 38 | + const handler1 = createErrorHandler(HttpErrorCodes.BAD_REQUEST, 'first'); |
| 39 | + const handler2 = createErrorHandler(HttpErrorCodes.NOT_FOUND, 'second'); |
| 40 | + |
| 41 | + // Act |
| 42 | + registry.register(CustomError, handler1); |
| 43 | + registry.register(CustomError, handler2); |
| 44 | + |
| 45 | + // Assess |
| 46 | + expect(console.warn).toHaveBeenCalledWith( |
| 47 | + 'Handler for CustomError already exists. The previous handler will be replaced.' |
| 48 | + ); |
| 49 | + |
| 50 | + const result = registry.resolve(new CustomError('test')); |
| 51 | + expect(result).toBe(handler2); |
| 52 | + }); |
| 53 | + |
| 54 | + it('registers handlers for multiple error types', () => { |
| 55 | + // Prepare |
| 56 | + const registry = new ErrorHandlerRegistry({ logger: console }); |
| 57 | + const handler = createErrorHandler(HttpErrorCodes.BAD_REQUEST); |
| 58 | + |
| 59 | + // Act |
| 60 | + registry.register([CustomError, AnotherError], handler); |
| 61 | + |
| 62 | + // Assess |
| 63 | + expect(registry.resolve(new CustomError('test'))).toBe(handler); |
| 64 | + expect(registry.resolve(new AnotherError('test'))).toBe(handler); |
| 65 | + }); |
| 66 | + |
| 67 | + it('resolves handlers using exact constructor match', () => { |
| 68 | + // Prepare |
| 69 | + const registry = new ErrorHandlerRegistry({ logger: console }); |
| 70 | + const customHandler = createErrorHandler(HttpErrorCodes.BAD_REQUEST); |
| 71 | + const anotherHandler = createErrorHandler( |
| 72 | + HttpErrorCodes.INTERNAL_SERVER_ERROR |
| 73 | + ); |
| 74 | + |
| 75 | + // Act |
| 76 | + registry.register(CustomError, customHandler); |
| 77 | + registry.register(AnotherError, anotherHandler); |
| 78 | + |
| 79 | + // Assess |
| 80 | + expect(registry.resolve(new CustomError('test'))).toBe(customHandler); |
| 81 | + expect(registry.resolve(new AnotherError('test'))).toBe(anotherHandler); |
| 82 | + }); |
| 83 | + |
| 84 | + it('resolves handlers using instanceof for inheritance', () => { |
| 85 | + // Prepare |
| 86 | + const registry = new ErrorHandlerRegistry({ logger: console }); |
| 87 | + const baseHandler = createErrorHandler(HttpErrorCodes.BAD_REQUEST); |
| 88 | + |
| 89 | + // Act |
| 90 | + registry.register(CustomError, baseHandler); |
| 91 | + |
| 92 | + // Assess |
| 93 | + const inheritedError = new InheritedError('test'); |
| 94 | + expect(registry.resolve(inheritedError)).toBe(baseHandler); |
| 95 | + }); |
| 96 | + |
| 97 | + it('resolves handlers using name-based matching', () => { |
| 98 | + // Prepare |
| 99 | + const registry = new ErrorHandlerRegistry({ logger: console }); |
| 100 | + const handler = createErrorHandler(HttpErrorCodes.BAD_REQUEST); |
| 101 | + |
| 102 | + // Act |
| 103 | + registry.register(CustomError, handler); |
| 104 | + |
| 105 | + const errorWithSameName = new Error('test'); |
| 106 | + errorWithSameName.name = 'CustomError'; |
| 107 | + |
| 108 | + // Assess |
| 109 | + expect(registry.resolve(errorWithSameName)).toBe(handler); |
| 110 | + }); |
| 111 | + |
| 112 | + it('returns null when no handler is found', () => { |
| 113 | + // Prepare |
| 114 | + const registry = new ErrorHandlerRegistry({ logger: console }); |
| 115 | + const handler = createErrorHandler(HttpErrorCodes.BAD_REQUEST); |
| 116 | + |
| 117 | + // Act |
| 118 | + registry.register(CustomError, handler); |
| 119 | + |
| 120 | + // Assess |
| 121 | + expect(registry.resolve(new AnotherError('test'))).toBeNull(); |
| 122 | + expect(registry.resolve(new Error('test'))).toBeNull(); |
| 123 | + }); |
| 124 | + |
| 125 | + it('prioritizes exact constructor match over instanceof', () => { |
| 126 | + // Prepare |
| 127 | + const registry = new ErrorHandlerRegistry({ logger: console }); |
| 128 | + const baseHandler = createErrorHandler(HttpErrorCodes.BAD_REQUEST); |
| 129 | + const specificHandler = createErrorHandler( |
| 130 | + HttpErrorCodes.INTERNAL_SERVER_ERROR |
| 131 | + ); |
| 132 | + |
| 133 | + // Act |
| 134 | + registry.register(CustomError, baseHandler); |
| 135 | + registry.register(InheritedError, specificHandler); |
| 136 | + |
| 137 | + // Assess |
| 138 | + expect(registry.resolve(new InheritedError('test'))).toBe(specificHandler); |
| 139 | + }); |
| 140 | + |
| 141 | + it('prioritizes instanceof match over name-based matching', () => { |
| 142 | + // Prepare |
| 143 | + const registry = new ErrorHandlerRegistry({ logger: console }); |
| 144 | + const baseHandler = createErrorHandler(HttpErrorCodes.BAD_REQUEST); |
| 145 | + const nameHandler = createErrorHandler( |
| 146 | + HttpErrorCodes.INTERNAL_SERVER_ERROR |
| 147 | + ); |
| 148 | + |
| 149 | + // Create a class with different name but register with name matching |
| 150 | + class DifferentNameError extends Error { |
| 151 | + constructor(message: string) { |
| 152 | + super(message); |
| 153 | + this.name = 'CustomError'; // Same name as CustomError |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + // Act |
| 158 | + registry.register(CustomError, baseHandler); |
| 159 | + registry.register(DifferentNameError, nameHandler); |
| 160 | + |
| 161 | + const error = new DifferentNameError('test'); |
| 162 | + |
| 163 | + // Assess |
| 164 | + expect(registry.resolve(error)).toBe(nameHandler); |
| 165 | + }); |
| 166 | +}); |
0 commit comments