Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 10 additions & 20 deletions packages/event-handler/src/rest/BaseRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { ResolveOptions } from '../types/index.js';
import type {
ErrorConstructor,
ErrorHandler,
ErrorResolveOptions,
HttpMethod,
Path,
RouteHandler,
Expand Down Expand Up @@ -157,12 +158,19 @@ abstract class BaseRouter {
*/
protected async handleError(
error: Error,
options?: ResolveOptions
options: ErrorResolveOptions
): Promise<Response> {
const handler = this.errorHandlerRegistry.resolve(error);
if (handler !== null) {
try {
const body = await handler.apply(options?.scope ?? this, [error]);
const body = await handler.apply(options.scope ?? this, [
error,
{
request: options.request,
event: options.event,
context: options.context,
},
]);
return new Response(JSON.stringify(body), {
status: body.statusCode,
headers: { 'Content-Type': 'application/json' },
Expand Down Expand Up @@ -273,24 +281,6 @@ abstract class BaseRouter {
): MethodDecorator | undefined {
return this.#handleHttpMethod(HttpVerbs.OPTIONS, path, handler);
}

public connect(path: Path, handler: RouteHandler): void;
public connect(path: Path): MethodDecorator;
public connect(
path: Path,
handler?: RouteHandler
): MethodDecorator | undefined {
return this.#handleHttpMethod(HttpVerbs.CONNECT, path, handler);
}

public trace(path: Path, handler: RouteHandler): void;
public trace(path: Path): MethodDecorator;
public trace(
path: Path,
handler?: RouteHandler
): MethodDecorator | undefined {
return this.#handleHttpMethod(HttpVerbs.TRACE, path, handler);
}
}

export { BaseRouter };
2 changes: 0 additions & 2 deletions packages/event-handler/src/rest/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
export const HttpVerbs = {
CONNECT: 'CONNECT',
TRACE: 'TRACE',
GET: 'GET',
POST: 'POST',
PUT: 'PUT',
Expand Down
30 changes: 20 additions & 10 deletions packages/event-handler/src/types/rest.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
import type { GenericLogger } from '@aws-lambda-powertools/commons/types';
import type {
GenericLogger,
JSONObject,
} from '@aws-lambda-powertools/commons/types';
import type { APIGatewayProxyEvent, Context } from 'aws-lambda';
import type { BaseRouter } from '../rest/BaseRouter.js';
import type { HttpErrorCodes, HttpVerbs } from '../rest/constants.js';
import type { Route } from '../rest/Route.js';
import type { ResolveOptions } from './common.js';

type ErrorResponse = {
statusCode: HttpStatusCode;
error: string;
message: string;
};

interface ErrorContext {
path: string;
method: string;
headers: Record<string, string>;
timestamp: string;
requestId?: string;
}
type RequestOptions = {
request: Request;
event: APIGatewayProxyEvent;
context: Context;
};

type ErrorResolveOptions = RequestOptions & ResolveOptions;

type ErrorHandler<T extends Error = Error> = (
error: T,
context?: ErrorContext
options?: RequestOptions
) => Promise<ErrorResponse>;

interface ErrorConstructor<T extends Error = Error> {
Expand Down Expand Up @@ -49,7 +54,10 @@ interface CompiledRoute {
type DynamicRoute = Route & CompiledRoute;

// biome-ignore lint/suspicious/noExplicitAny: we want to keep arguments and return types as any to accept any type of function
type RouteHandler<T = any, R = any> = (...args: T[]) => R;
type RouteHandler<
TParams = Record<string, unknown>,
TReturn = Response | JSONObject,
> = (args: TParams, options?: RequestOptions) => Promise<TReturn>;

type HttpMethod = keyof typeof HttpVerbs;

Expand Down Expand Up @@ -98,9 +106,11 @@ export type {
ErrorConstructor,
ErrorHandlerRegistryOptions,
ErrorHandler,
ErrorResolveOptions,
HttpStatusCode,
HttpMethod,
Path,
RequestOptions,
RouterOptions,
RouteHandler,
RouteOptions,
Expand Down
Loading