diff --git a/packages/event-handler/src/http/errors.ts b/packages/event-handler/src/http/errors.ts index a735b4d806..d241464de5 100644 --- a/packages/event-handler/src/http/errors.ts +++ b/packages/event-handler/src/http/errors.ts @@ -228,7 +228,7 @@ class InvalidEventError extends Error { class InvalidHttpMethodError extends Error { constructor(method: string) { super(`HTTP method ${method} is not supported.`); - this.name = 'InvalidEventError'; + this.name = 'InvalidHttpMethodError'; } } diff --git a/packages/event-handler/tests/unit/http/converters.test.ts b/packages/event-handler/tests/unit/http/converters.test.ts index cf673e5e41..4ec64b5a29 100644 --- a/packages/event-handler/tests/unit/http/converters.test.ts +++ b/packages/event-handler/tests/unit/http/converters.test.ts @@ -8,6 +8,7 @@ import { bodyToNodeStream, webHeadersToApiGatewayHeaders, } from '../../../src/http/converters.js'; +import { InvalidHttpMethodError } from '../../../src/http/errors.js'; import { HttpStatusCodes, handlerResultToWebResponse, @@ -773,6 +774,43 @@ describe('Converters', () => { }); }); + describe('proxyEventToWebRequest (unsupported HTTP method)', () => { + it.each([ + { + version: 'V1', + createEvent: () => createTestEvent('/test', 'CONNECT'), + }, + { + version: 'V2', + createEvent: () => createTestEventV2('/test', 'CONNECT'), + }, + { + version: 'ALB', + createEvent: () => createTestALBEvent('/test', 'CONNECT'), + }, + ])('throws InvalidHttpMethodError with the correct name for $version events', ({ + createEvent, + }) => { + // Prepare + const event = createEvent(); + + // Act & Assess + expect(() => proxyEventToWebRequest(event)).toThrow( + InvalidHttpMethodError + ); + + try { + proxyEventToWebRequest(event); + } catch (err) { + expect(err).toBeInstanceOf(InvalidHttpMethodError); + expect((err as Error).name).toBe('InvalidHttpMethodError'); + expect((err as Error).message).toBe( + 'HTTP method CONNECT is not supported.' + ); + } + }); + }); + describe('responseToProxyResult', () => { it('converts basic Response to API Gateway result', async () => { // Prepare diff --git a/packages/event-handler/tests/unit/http/errors.test.ts b/packages/event-handler/tests/unit/http/errors.test.ts index 63e32d9708..293bc0e83f 100644 --- a/packages/event-handler/tests/unit/http/errors.test.ts +++ b/packages/event-handler/tests/unit/http/errors.test.ts @@ -1,4 +1,8 @@ import { describe, expect, it } from 'vitest'; +import { + InvalidEventError, + InvalidHttpMethodError, +} from '../../../src/http/errors.js'; import { BadRequestError, ForbiddenError, @@ -80,6 +84,7 @@ describe('HTTP Error Classes', () => { expect(error.message).toBe(customMessage); expect(error.statusCode).toBe(statusCode); expect(error.errorType).toBe(errorType); + expect(error.name).toBe(errorType); }); describe('toJSON', () => { @@ -379,6 +384,26 @@ describe('HTTP Error Classes', () => { }); }); + describe('Invalid* errors (non-HttpError subclasses)', () => { + it('InvalidEventError carries its own name and message', () => { + const error = new InvalidEventError('bad event'); + + expect(error).toBeInstanceOf(Error); + expect(error).toBeInstanceOf(InvalidEventError); + expect(error.name).toBe('InvalidEventError'); + expect(error.message).toBe('bad event'); + }); + + it('InvalidHttpMethodError carries its own name and a method-aware message', () => { + const error = new InvalidHttpMethodError('CONNECT'); + + expect(error).toBeInstanceOf(Error); + expect(error).toBeInstanceOf(InvalidHttpMethodError); + expect(error.name).toBe('InvalidHttpMethodError'); + expect(error.message).toBe('HTTP method CONNECT is not supported.'); + }); + }); + describe('ResponseValidationError', () => { it('creates error with correct statusCode', () => { const error = new ResponseValidationError(