From 7daa82fc7aacb717ec22ce6ce95c3d2e0074390e Mon Sep 17 00:00:00 2001 From: Liran Tal Date: Fri, 11 Aug 2023 16:18:31 +0300 Subject: [PATCH] fix: avoid crashing the Node.js app on uncaught exceptions (#307) * fix: avoid crashing the Node.js app on uncaught exceptions * Create smooth-dodos-obey.md --------- Co-authored-by: Eric Allam --- .changeset/smooth-dodos-obey.md | 5 +++++ packages/express/src/index.ts | 19 +++++++++++-------- 2 files changed, 16 insertions(+), 8 deletions(-) create mode 100644 .changeset/smooth-dodos-obey.md diff --git a/.changeset/smooth-dodos-obey.md b/.changeset/smooth-dodos-obey.md new file mode 100644 index 000000000..bd7f40e4b --- /dev/null +++ b/.changeset/smooth-dodos-obey.md @@ -0,0 +1,5 @@ +--- +"@trigger.dev/express": patch +--- + +fix: avoid crashing the Node.js app on uncaught exceptions diff --git a/packages/express/src/index.ts b/packages/express/src/index.ts index e2d2a0d81..00618df6f 100644 --- a/packages/express/src/index.ts +++ b/packages/express/src/index.ts @@ -66,18 +66,21 @@ export function createMiddleware(client: TriggerClient, path: string = "/api/tri return; } - const request = convertToStandardRequest(req); + try { + const request = convertToStandardRequest(req); - const response = await client.handleRequest(request); + const response = await client.handleRequest(request); - if (!response) { - res.status(404).json({ error: "Not found" }); + if (!response) { + res.status(404).json({ error: "Not found" }); - return; - } + return; + } - res.status(response.status).json(response.body); - }; + res.status(response.status).json(response.body); + } catch (error) { + next(error) + }; } function convertToStandardRequest(req: express.Request): StandardRequest {