Skip to content

Commit 696d10f

Browse files
committed
removed the ErrorResponse and used HandlerResponse
1 parent e6b5cf2 commit 696d10f

File tree

5 files changed

+21
-39
lines changed

5 files changed

+21
-39
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
isDevMode,
55
} from '@aws-lambda-powertools/commons/utils/env';
66
import type { APIGatewayProxyResult, Context } from 'aws-lambda';
7-
import type { ResolveOptions } from '../types/index.js';
7+
import type { HandlerResponse, ResolveOptions } from '../types/index.js';
88
import type {
99
ErrorConstructor,
1010
ErrorHandler,
@@ -22,7 +22,6 @@ import {
2222
handlerResultToProxyResult,
2323
handlerResultToWebResponse,
2424
proxyEventToWebRequest,
25-
webResponseToProxyResult,
2625
} from './converters.js';
2726
import { ErrorHandlerRegistry } from './ErrorHandlerRegistry.js';
2827
import {
@@ -36,6 +35,7 @@ import { RouteHandlerRegistry } from './RouteHandlerRegistry.js';
3635
import {
3736
composeMiddleware,
3837
isAPIGatewayProxyEvent,
38+
isAPIGatewayProxyResult,
3939
isHttpMethod,
4040
} from './utils.js';
4141

@@ -280,7 +280,12 @@ class Router {
280280
...requestContext,
281281
scope: options?.scope,
282282
});
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+
);
284289
}
285290
}
286291

@@ -310,13 +315,13 @@ class Router {
310315
protected async handleError(
311316
error: Error,
312317
options: ErrorResolveOptions
313-
): Promise<Response> {
318+
): Promise<HandlerResponse> {
314319
const handler = this.errorHandlerRegistry.resolve(error);
315320
if (handler !== null) {
316321
try {
317322
const { scope, ...reqCtx } = options;
318323
const body = await handler.apply(scope ?? this, [error, reqCtx]);
319-
if (body instanceof Response) {
324+
if (body instanceof Response || isAPIGatewayProxyResult(body)) {
320325
return body;
321326
}
322327
if (!body.statusCode) {

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

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

66
/**
@@ -181,10 +181,12 @@ export const handlerResultToWebResponse = (
181181
* Handles APIGatewayProxyResult, Response objects, and plain objects.
182182
*
183183
* @param response - The handler response (APIGatewayProxyResult, Response, or plain object)
184+
* @param statusCode - The response status code to return
184185
* @returns An API Gateway proxy result
185186
*/
186187
export const handlerResultToProxyResult = async (
187-
response: HandlerResponse
188+
response: HandlerResponse,
189+
statusCode: (typeof HttpErrorCodes)[keyof typeof HttpErrorCodes] = HttpErrorCodes.OK
188190
): Promise<APIGatewayProxyResult> => {
189191
if (isAPIGatewayProxyResult(response)) {
190192
return response;
@@ -193,7 +195,7 @@ export const handlerResultToProxyResult = async (
193195
return await webResponseToProxyResult(response);
194196
}
195197
return {
196-
statusCode: 200,
198+
statusCode,
197199
body: JSON.stringify(response),
198200
headers: { 'content-type': 'application/json' },
199201
isBase64Encoded: false,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ErrorResponse, HttpStatusCode } from '../types/rest.js';
1+
import type { HandlerResponse, HttpStatusCode } from '../types/rest.js';
22
import { HttpErrorCodes } from './constants.js';
33

44
export class RouteMatchingError extends Error {
@@ -34,7 +34,7 @@ export abstract class ServiceError extends Error {
3434
this.details = details;
3535
}
3636

37-
toJSON(): ErrorResponse {
37+
toJSON(): HandlerResponse {
3838
return {
3939
statusCode: this.statusCode,
4040
error: this.errorType,

packages/event-handler/src/types/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ export type {
3535
CorsOptions,
3636
ErrorHandler,
3737
ErrorResolveOptions,
38-
ErrorResponse,
3938
HandlerResponse,
4039
HttpMethod,
4140
HttpStatusCode,

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

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,11 @@ import type {
33
JSONObject,
44
} from '@aws-lambda-powertools/commons/types';
55
import type { APIGatewayProxyEvent, Context } from 'aws-lambda';
6-
import type {
7-
MethodNotAllowedError,
8-
NotFoundError,
9-
} from '../../src/rest/errors.js';
106
import type { HttpErrorCodes, HttpVerbs } from '../rest/constants.js';
117
import type { Route } from '../rest/Route.js';
128
import type { Router } from '../rest/Router.js';
139
import type { ResolveOptions } from './common.js';
1410

15-
type ErrorResponse =
16-
| {
17-
statusCode: HttpStatusCode;
18-
error: string;
19-
message?: string;
20-
}
21-
| {
22-
statusCode: HttpStatusCode;
23-
error?: string;
24-
message: string;
25-
};
26-
2711
type RequestContext = {
2812
req: Request;
2913
event: APIGatewayProxyEvent;
@@ -34,17 +18,10 @@ type RequestContext = {
3418

3519
type ErrorResolveOptions = RequestContext & ResolveOptions;
3620

37-
type ErrorHandler<T extends Error = Error> = T extends
38-
| NotFoundError
39-
| MethodNotAllowedError
40-
? (
41-
error: T,
42-
reqCtx: RequestContext
43-
) => Promise<Omit<ErrorResponse, 'statusCode'> | Response | JSONObject>
44-
: (
45-
error: T,
46-
reqCtx: RequestContext
47-
) => Promise<ErrorResponse | Response | JSONObject>;
21+
type ErrorHandler<T extends Error = Error> = (
22+
error: T,
23+
reqCtx: RequestContext
24+
) => Promise<HandlerResponse>;
4825

4926
interface ErrorConstructor<T extends Error = Error> {
5027
new (...args: any[]): T;
@@ -182,7 +159,6 @@ export type {
182159
CompiledRoute,
183160
CorsOptions,
184161
DynamicRoute,
185-
ErrorResponse,
186162
ErrorConstructor,
187163
ErrorHandlerRegistryOptions,
188164
ErrorHandler,

0 commit comments

Comments
 (0)