4
4
isDevMode ,
5
5
} from '@aws-lambda-powertools/commons/utils/env' ;
6
6
import type { APIGatewayProxyResult , Context } from 'aws-lambda' ;
7
- import type { ResolveOptions } from '../types/index.js' ;
7
+ import type { HandlerResponse , ResolveOptions } from '../types/index.js' ;
8
8
import type {
9
9
ErrorConstructor ,
10
10
ErrorHandler ,
@@ -22,7 +22,6 @@ import {
22
22
handlerResultToProxyResult ,
23
23
handlerResultToWebResponse ,
24
24
proxyEventToWebRequest ,
25
- webResponseToProxyResult ,
26
25
} from './converters.js' ;
27
26
import { ErrorHandlerRegistry } from './ErrorHandlerRegistry.js' ;
28
27
import {
@@ -36,6 +35,7 @@ import { RouteHandlerRegistry } from './RouteHandlerRegistry.js';
36
35
import {
37
36
composeMiddleware ,
38
37
isAPIGatewayProxyEvent ,
38
+ isAPIGatewayProxyResult ,
39
39
isHttpMethod ,
40
40
} from './utils.js' ;
41
41
@@ -280,7 +280,12 @@ class Router {
280
280
...requestContext ,
281
281
scope : options ?. scope ,
282
282
} ) ;
283
- return await webResponseToProxyResult ( result ) ;
283
+ return handlerResultToProxyResult (
284
+ result ,
285
+ ( result . status ??
286
+ result . statusCode ??
287
+ HttpErrorCodes . INTERNAL_SERVER_ERROR ) as ( typeof HttpErrorCodes ) [ keyof typeof HttpErrorCodes ]
288
+ ) ;
284
289
}
285
290
}
286
291
@@ -310,13 +315,13 @@ class Router {
310
315
protected async handleError (
311
316
error : Error ,
312
317
options : ErrorResolveOptions
313
- ) : Promise < Response > {
318
+ ) : Promise < HandlerResponse > {
314
319
const handler = this . errorHandlerRegistry . resolve ( error ) ;
315
320
if ( handler !== null ) {
316
321
try {
317
322
const { scope, ...reqCtx } = options ;
318
323
const body = await handler . apply ( scope ?? this , [ error , reqCtx ] ) ;
319
- if ( body instanceof Response ) {
324
+ if ( body instanceof Response || isAPIGatewayProxyResult ( body ) ) {
320
325
return body ;
321
326
}
322
327
if ( ! body . statusCode ) {
0 commit comments