#ExceptionsHandler magic
1 messages · Page 1 of 1 (latest)
What would you prefer to happen? Let the application crash because you didn't handle the error?
Maybe you just need to read the docs first https://docs.nestjs.com/exception-filters
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).
Yes and it really doesn't matter, it's a dev responsibility, not a platform responsibility.
Having a call inside a try-catch that gets caught somehow-somewhere doesn't create any fluidity at all.
It's counter-intuitive at any sort of level.
Okay. Tell me what you'd expect to happen with this express route:
app.get(async (req, res, next) => {
try {
await someAsyncMethodThtThrows();
} catch (err) {
throw err;
}
})
That'll crash the server completely. No subsequent requests can be made, dev intention is needed, if this is in production and without auto restart via some process manager, your server is now down and that could be costing the business money, right?
Maybe you'd like to write everything from scratch, without the help of any framework then. Everything that the program does is the programmers responsibility 😉 FW can help with a lot of these things, but you need to understand how the FW works first.
Now of course, it's possible to add an error handling middleware.
That's essentially what the exception filters are
And nest's default is the barebones logic to handle HttpException classes to make sure error codes are propagated and to handle unknown errors to ensure the server stays running while providing logs for the devs to look at
It's not a problem of lack of respect towards the community and contributors.
It's a common sense question and a partial rant, because it makes no sense to use "magic".
How do I remove it?
What's magical about it?
Why would you want to remove the safety from the gun pointed at your foot?
In every bare express server, one of the first things you'd do is bind the error handling middleware. Here, one is provided for you and you're trying to remove it??
That said, if you read the piece of documentation about exception filters I shared above, you'll learn how to bind a custom exception filters with any kind of error handling logic (or lack thereof) you like
yes.
ok. thank you.
Great.. Awesome.. Thanks for elaborating about what is magical
//service call that throws an error
} catch (e) { /**/ }
If that error doesn't end up in my catch, it's magic.
That shouldn't ever happen though, and if it does it's most likely misusing the underlying language, not the framework itself. Without seeing actual code though, I can't say what happened
The error should only ever end up in the exception filter if it's not handled in your code. It should always end up in your catch, and only end up in the exception filtery if you subsequently throw from the catch.
Nest won't just intercept the error from your service unless it's re-thrown from the controller or there's no catch in the first place
// in the service method called bellow
throw new Error('catch me');
// in a controller method
try {
//service call that throws an error
} catch (e) { /**/ }
Is this async, is there an await missing, what's the route handler look like, we still don't know what we're looking at with your code
// UsersService
async create(createUserDto: CreateUserDto): Promise<void> { throw new Error('catch me');
}
// UsersController
@Post() create(@Body() createUserDto: CreateUserDto) { try {
return this.usersService.create(createUserDto);
} catch (e) {
console.log(e); // not happening
}
}
// effect
[backend] [Nest] 491 - 03/25/2023, 8:07:41 AM ERROR [ExceptionsHandler] catch me
[backend] Error: catch me
// Note: if I remove the return from "return this.usersService.create(createUserDto);" I'm still getting an error logged, but it's still not being caught by the catch
[backend] /app/src/users/users.service.ts:20
[backend] throw new Error('catch me');
[backend] ^
[backend] Error: catch me
Do return await this.usersServive.create()
It's a difference of when the promise is resolved.
Makes sense, but my catch is useless 🥹
Doing return promise makes the promise resolve after the try/catch , but return await promise makes the promise resolve before returning, so still within the bound context and the exception can be caught
yep. works well
That error you were getting when you removed return is because there's nothing to wait for the resolving promise and the exception happens outside of the scope of the request
ty 🥹