#ExceptionsHandler magic

1 messages · Page 1 of 1 (latest)

zealous bridge
#

I would understand if this would happen if i wouldn't have a try-catch, but having a try-catch and being caught "somehow" makes no sense.

#

ExceptionsHandler magic

north cairn
#

What would you prefer to happen? Let the application crash because you didn't handle the error?

violet flare
zealous bridge
#

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.

north cairn
#

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?

violet flare
#

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.

north cairn
#

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

zealous bridge
north cairn
#

What's magical about it?

#

Why would you want to remove the safety from the gun pointed at your foot?

violet flare
#

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

zealous bridge
north cairn
zealous bridge
north cairn
#

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

violet flare
#

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.

north cairn
#

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

zealous bridge
#
// in the service method called bellow
throw new Error('catch me');

// in a controller method
try {
  //service call that throws an error
} catch (e) { /**/ }
north cairn
#

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

zealous bridge
#
// 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
north cairn
#

Do return await this.usersServive.create()

#

It's a difference of when the promise is resolved.

zealous bridge
#

Makes sense, but my catch is useless 🥹

north cairn
#

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

zealous bridge
#

yep. works well

north cairn
#

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

zealous bridge
#

ty 🥹