nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.69k stars 7.63k forks source link

Error thrown by INestApplicationContext.select() not caught #13033

Closed RocheVal closed 4 months ago

RocheVal commented 10 months ago

Is there an existing issue for this?

Current behavior

When using INestApplicationContext.select to select a module that is NOT loaded in the app an error is thrown (Nest could not select the given module (it does not exist in current context)). But this error is not caught by try/catch, preventing us to handle the error manually, resulting in crashing the app.

You can see in the stackblitz provided that the console.log located in the main.ts is not triggered when a module is not found.

I have discussed this on discord, and we concluded it might be a bug.

Minimum reproduction code

https://stackblitz.com/edit/nestjs-starter-rgdzro?file=src%2Fmain.ts

Steps to reproduce

In your main.ts:

try {
    app.select(NotLoadedModule);
  } catch (e) {
    console.log('Module not loaded');
  }

Or just run the stackblitz provided.

Expected behavior

I expect the error thrown by the select to be caught by the try/catch.

Package

Other package

No response

NestJS version

10.2.10

Packages versions

@nestjs/10.2.10

Node.js version

18.16.0

In which operating systems have you tested?

Other

No response

yannkaiser commented 9 months ago

This is due to the exceptionProxy when you call NestFactory.create, see: https://github.com/nestjs/nest/blob/180bdf67786f9b8e917e9d2f5e9d9598f7f40867/packages/core/nest-factory.ts#L268 There is an option you can pass to create to not abort on error, would that be a solution to your issue ?

i.e.:

  const app = await NestFactory.create(AppModule, { abortOnError: false });
  try {
    app.select(NotLoadedModule);
  } catch (e) {
    console.log('Module not loaded');
  }
RocheVal commented 9 months ago

Okay that's why.

I didn't know about abortOnError. But I would like to keep the abortOnError behavior for the all the other errors. It's just when selecting a non existing module that I would like to be able to catch and handle the error myself.

To be clear, this problem is not blocking for me (I found some workarounds) but I thought there might be an issue with the select that's why I created this issue.

In my opinion it would be nice to have this behavior, but I let you see if you want to change it or not. I don't know if it's worth it.

Thanks for your answer !

micalevisk commented 4 months ago

so we have two things here:

  1. abortOnError: false will allow us to catch that exception. So what you're reporting is not a bug
  2. We'll see the error message on console even when we're handling the exception because of the following line

https://github.com/nestjs/nest/blob/15cb568e40f42fb3c40c4cb2ad432b23a5ec7bcd/packages/core/errors/exceptions-zone.ts#L17

I'm not sure if [2] is a bug, tho.


@RocheVal what do you think about introducing a new options arg to app.select so we could supply abortOnError: false to it while keeping the abortOnError: true for the rest?

RocheVal commented 4 months ago

Sorry for the late reply.

@micalevisk introducing this new options seems a good idea to me. I would be able to handle the presence (or not) of a module in a clean way.

micalevisk commented 4 months ago

@RocheVal please open a feature request issue for that

RocheVal commented 4 months ago

Done https://github.com/nestjs/nest/issues/13735