File tree Expand file tree Collapse file tree 3 files changed +40
-7
lines changed Expand file tree Collapse file tree 3 files changed +40
-7
lines changed Original file line number Diff line number Diff line change @@ -4,7 +4,11 @@ import {
4
4
isDevMode ,
5
5
} from '@aws-lambda-powertools/commons/utils/env' ;
6
6
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' ;
8
12
import type {
9
13
ErrorConstructor ,
10
14
ErrorHandler ,
@@ -282,10 +286,7 @@ class Router {
282
286
} ) ;
283
287
const statusCode =
284
288
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 ) ;
289
290
}
290
291
}
291
292
Original file line number Diff line number Diff line change 1
1
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' ;
3
7
import { COMPRESSION_ENCODING_TYPES , HttpErrorCodes } from './constants.js' ;
4
8
import { isAPIGatewayProxyResult } from './utils.js' ;
5
9
@@ -186,7 +190,7 @@ export const handlerResultToWebResponse = (
186
190
*/
187
191
export const handlerResultToProxyResult = async (
188
192
response : HandlerResponse ,
189
- statusCode : ( typeof HttpErrorCodes ) [ keyof typeof HttpErrorCodes ] = HttpErrorCodes . OK
193
+ statusCode : HttpStatusCode = HttpErrorCodes . OK
190
194
) : Promise < APIGatewayProxyResult > => {
191
195
if ( isAPIGatewayProxyResult ( response ) ) {
192
196
return response ;
Original file line number Diff line number Diff line change @@ -539,4 +539,32 @@ describe('Class: Router - Error Handling', () => {
539
539
isBase64Encoded : false ,
540
540
} ) ;
541
541
} ) ;
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
+ } ) ;
542
570
} ) ;
You can’t perform that action at this time.
0 commit comments