Skip to content

Commit 5ee4241

Browse files
committed
Changed the HttpStatusCode type and added test to throw a generic error
1 parent aa9dbe9 commit 5ee4241

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

packages/event-handler/src/rest/Router.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import {
44
isDevMode,
55
} from '@aws-lambda-powertools/commons/utils/env';
66
import type { APIGatewayProxyResult, Context } from 'aws-lambda';
7-
import type { HandlerResponse, ResolveOptions } from '../types/index.js';
7+
import type {
8+
HandlerResponse,
9+
HttpStatusCode,
10+
ResolveOptions,
11+
} from '../types/index.js';
812
import type {
913
ErrorConstructor,
1014
ErrorHandler,
@@ -282,10 +286,7 @@ class Router {
282286
});
283287
const statusCode =
284288
result instanceof Response ? result.status : result.statusCode;
285-
return handlerResultToProxyResult(
286-
result,
287-
statusCode as (typeof HttpErrorCodes)[keyof typeof HttpErrorCodes]
288-
);
289+
return handlerResultToProxyResult(result, statusCode as HttpStatusCode);
289290
}
290291
}
291292

packages/event-handler/src/rest/converters.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import type { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
2-
import type { CompressionOptions, HandlerResponse } from '../types/rest.js';
2+
import type {
3+
CompressionOptions,
4+
HandlerResponse,
5+
HttpStatusCode,
6+
} from '../types/rest.js';
37
import { COMPRESSION_ENCODING_TYPES, HttpErrorCodes } from './constants.js';
48
import { isAPIGatewayProxyResult } from './utils.js';
59

@@ -186,7 +190,7 @@ export const handlerResultToWebResponse = (
186190
*/
187191
export const handlerResultToProxyResult = async (
188192
response: HandlerResponse,
189-
statusCode: (typeof HttpErrorCodes)[keyof typeof HttpErrorCodes] = HttpErrorCodes.OK
193+
statusCode: HttpStatusCode = HttpErrorCodes.OK
190194
): Promise<APIGatewayProxyResult> => {
191195
if (isAPIGatewayProxyResult(response)) {
192196
return response;

packages/event-handler/tests/unit/rest/Router/error-handling.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,4 +539,32 @@ describe('Class: Router - Error Handling', () => {
539539
isBase64Encoded: false,
540540
});
541541
});
542+
543+
it('handles throwing a generic error from the error handler', async () => {
544+
// Prepare
545+
const app = new Router();
546+
547+
app.errorHandler(BadRequestError, async () => {
548+
throw new Error('This error is thrown from the error handler');
549+
});
550+
551+
app.get('/test', () => {
552+
throw new BadRequestError('test error');
553+
});
554+
555+
// Act
556+
const result = await app.resolve(createTestEvent('/test', 'GET'), context);
557+
558+
// Assess
559+
expect(result).toEqual({
560+
statusCode: HttpErrorCodes.INTERNAL_SERVER_ERROR,
561+
body: JSON.stringify({
562+
statusCode: HttpErrorCodes.INTERNAL_SERVER_ERROR,
563+
error: 'Internal Server Error',
564+
message: 'Internal Server Error',
565+
}),
566+
headers: { 'content-type': 'application/json' },
567+
isBase64Encoded: false,
568+
});
569+
});
542570
});

0 commit comments

Comments
 (0)