File tree Expand file tree Collapse file tree 3 files changed +31
-7
lines changed Expand file tree Collapse file tree 3 files changed +31
-7
lines changed Original file line number Diff line number Diff line change @@ -327,14 +327,11 @@ class Router {
327
327
}
328
328
}
329
329
return new Response ( JSON . stringify ( body ) , {
330
- status : body . statusCode ,
330
+ status : ( body . statusCode as number ) ?? HttpErrorCodes . OK ,
331
331
headers : { 'Content-Type' : 'application/json' } ,
332
332
} ) ;
333
333
} catch ( handlerError ) {
334
- if ( handlerError instanceof NotFoundError ) {
335
- return await this . handleError ( handlerError , options ) ;
336
- }
337
- if ( handlerError instanceof MethodNotAllowedError ) {
334
+ if ( handlerError instanceof ServiceError ) {
338
335
return await this . handleError ( handlerError , options ) ;
339
336
}
340
337
return this . #defaultErrorHandler( handlerError as Error ) ;
Original file line number Diff line number Diff line change @@ -40,8 +40,11 @@ type ErrorHandler<T extends Error = Error> = T extends
40
40
? (
41
41
error : T ,
42
42
reqCtx : RequestContext
43
- ) => Promise < Omit < ErrorResponse , 'statusCode' > | Response >
44
- : ( error : T , reqCtx : RequestContext ) => Promise < ErrorResponse | Response > ;
43
+ ) => Promise < Omit < ErrorResponse , 'statusCode' > | Response | JSONObject >
44
+ : (
45
+ error : T ,
46
+ reqCtx : RequestContext
47
+ ) => Promise < ErrorResponse | Response | JSONObject > ;
45
48
46
49
interface ErrorConstructor < T extends Error = Error > {
47
50
new ( ...args : any [ ] ) : T ;
Original file line number Diff line number Diff line change @@ -431,6 +431,30 @@ describe('Class: Router - Error Handling', () => {
431
431
} ) ;
432
432
} ) ;
433
433
434
+ it ( 'handles returning a JSONObject from the error handler' , async ( ) => {
435
+ // Prepare
436
+ const app = new Router ( ) ;
437
+
438
+ app . errorHandler ( BadRequestError , async ( ) => ( { foo : 'bar' } ) ) ;
439
+
440
+ app . get ( '/test' , ( ) => {
441
+ throw new BadRequestError ( 'test error' ) ;
442
+ } ) ;
443
+
444
+ // Act
445
+ const result = await app . resolve ( createTestEvent ( '/test' , 'GET' ) , context ) ;
446
+
447
+ // Assess
448
+ expect ( result ) . toEqual ( {
449
+ statusCode : HttpErrorCodes . OK ,
450
+ body : JSON . stringify ( {
451
+ foo : 'bar' ,
452
+ } ) ,
453
+ headers : { 'content-type' : 'application/json' } ,
454
+ isBase64Encoded : false ,
455
+ } ) ;
456
+ } ) ;
457
+
434
458
it ( 'handles throwing a built in NotFound error from the error handler' , async ( ) => {
435
459
// Prepare
436
460
const app = new Router ( ) ;
You can’t perform that action at this time.
0 commit comments