Skip to content

Commit 37e0797

Browse files
committed
change lambda handelr error
1 parent 7431c47 commit 37e0797

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/api/lambda.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import "zod-openapi/extend";
2-
import awsLambdaFastify from "@fastify/aws-lambda";
2+
import awsLambdaFastify, { LambdaResponse } from "@fastify/aws-lambda";
33
import init from "./index.js";
44
import warmer from "lambda-warmer";
55
import { type APIGatewayEvent, type Context } from "aws-lambda";
6+
import { InternalServerError } from "common/errors/index.js";
67

78
const app = await init();
89
const realHandler = awsLambdaFastify(app, {
@@ -16,7 +17,23 @@ const handler = async (event: APIGatewayEvent, context: Context) => {
1617
return "warmed";
1718
}
1819
// else proceed with handler logic
19-
return realHandler(event, context);
20+
try {
21+
return realHandler(event, context);
22+
} catch (e) {
23+
console.error(e);
24+
const newError = new InternalServerError({
25+
message: "Failed to initialize application.",
26+
});
27+
const json = JSON.stringify(newError.toJson());
28+
return {
29+
statusCode: newError.httpStatusCode,
30+
body: json,
31+
headers: {
32+
"Content-Type": "application/json",
33+
},
34+
isBase64Encoded: false,
35+
};
36+
}
2037
};
2138

2239
await app.ready(); // needs to be placed after awsLambdaFastify call because of the decoration: https://github.com/fastify/aws-lambda-fastify/blob/master/index.js#L9

0 commit comments

Comments
 (0)