Skip to content
2 changes: 1 addition & 1 deletion packages/event-handler/src/http/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}

Expand Down
38 changes: 38 additions & 0 deletions packages/event-handler/tests/unit/http/converters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
bodyToNodeStream,
webHeadersToApiGatewayHeaders,
} from '../../../src/http/converters.js';
import { InvalidHttpMethodError } from '../../../src/http/errors.js';
import {
HttpStatusCodes,
handlerResultToWebResponse,
Expand Down Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions packages/event-handler/tests/unit/http/errors.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { describe, expect, it } from 'vitest';
import {
InvalidEventError,
InvalidHttpMethodError,
} from '../../../src/http/errors.js';
import {
BadRequestError,
ForbiddenError,
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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(
Expand Down
Loading