While using app.useGlobalFilters(new MongoExceptionFilter());
in main.ts
in a NestJS app (or a local controller MongoExceptionFilter), the MongoException is not getting caught whenever I call an async function without await -- it just crashes the NestJS app with a MongoException
async funcA
await funcB // works well, MongoExceptionFilter does its job
funcB // crashes the app, why doesn't MongoExceptionFilter catch this?
async funcB
// throws a MongoException
tried to call the func with await - it worked but wouldn't work without await
CodePudding user response:
If you don't await an asynchronous function, the promise is unhandled and the method is considered to be called synchronously. Because of this, the response can be sent to the client ending the lifecycle of the request and Nest's try/catch
around anything that happens within the request. Think of it as an express middleware. Essentially, you've called the next
callback without indicating there's an error, and then an error happens later on. Because the execution stack has already moved on it's impossible for the error handler to know to listen for that exception