Uncaught error in async endpoint crashes express #2340
Replies: 2 comments
-
Hey there! This is a great topic and the amount of information you've provided here is awesome. I think personally the way to go would be your Option 2:
But I wonder, because having a Would certainly clean up custom handlers if you don't have to wrap every single one in a top-level try / catch. I would love to get more feedback on this idea. I'll take it to my team and share it with Discord. I will also convert this to a Discussion and tag it as a Feature Request. Thanks for all of the notes. The plugin is great too! |
Beta Was this translation helpful? Give feedback.
-
This can be solved with just two lines:
For example, my server.ts starts like this (this also applies to JS):
|
Beta Was this translation helpful? Give feedback.
-
Bug Report
Any uncaught errors in an async
handler
function of an endpoint will crash the underlying express instance (however not the process as it seems). I understand that this is something that is flawed in express and should be fixed upstream however the Payload docs themselves use an async endpoint in the docs without any mention of this issue so I feel like it should be handled until express 5 becomes stable.Workaround in the form of a plugin is included below.
Steps to Reproduce
Other Details
After some digging, it turns out that Express 4 does not handle errors in async route handlers which will be fixed in Express 5 however that's been in beta for a year now (related issue with maintainer updates: expressjs/express#4920).
My two ideas for resolution of this would be:
And then using that function in here (maybe somewhere else, I haven't looked far and wide but this seemed like a reasonable guess):
payload/src/express/mountEndpoints.ts
Lines 4 to 12 in 4d578f1
Payload version: 1.6.17
Workaround (Payload plugin)
Using the function from above, it's possible to write a plugin as workaround that handles async errors gracefully:
Beta Was this translation helpful? Give feedback.
All reactions