Closed mhvelplund closed 1 year ago
Hello there mhvelplund 👋
Thank you for opening your very first issue in this project.
We will try to get back to you as soon as we can.👀
I feel like an idiot. After wasting 2+ hours of my weekend on this and creating this issue, I found a ugly workaround and moved on. A little later I ran the linter and found the reason for the previous problem. The root exception originated in an unawaited async method.
Would it be possible to add a note to the starter guide saying that exceptions in unhandled promises escape the error handler and crash Express?
(obviously this issue can be closed)
This is one of the major caveats of express (unability to properly deal with async/await).
The docs repo is here if you want to add a small note to the guide: https://github.com/tsoa-community/docs
Edit: found the reason; I had an exception originating in an unawaited promise 🤦♂️
I'm having some issues with the global error handler when using TSOA. I followed the guide here and created an error handler that looks like this:
Next, I created a controller that looks like this:
Everything worked fine for a while, but hen I notice that in some cases the error handler is not called. I can be more specific if needed, but essentially if
this.logService.getServiceLogs()
throws an exception it crashes express.Express logs:
But if I change the controller to just throw everytime, the error handler works, and a 401 HTTP response.:
Simplified controller:
Express logs:
I read through the Express documentation and it seems you are not allowed to throw exceptions in
async
functions. Instead you are supposed to call thenext()
function with them. The TSOA controller doesn't give me that option, and the odd thing is that I'm 90% sure that it actually caught some of the other exceptions before, inside the async code.So my question is, are there any limitations on the global error handling that I'm unaware of? Is the handler supposed to be able to trap exceptions thrown inside async functions, and if so, why might it be failing here?
Steps to Reproduce
See above.
Context (Environment)
Version of the library: 5.1.1 Version of NodeJS: 18.16.0
Detailed Description
See above.