Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
3 of 15 tasks
RocheVal opened this issue Jan 10, 2024 · 6 comments
Closed
3 of 15 tasks

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

RocheVal opened this issue Jan 10, 2024 · 6 comments
Labels
needs triage This issue has not been looked into

Comments

@RocheVal
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

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

  • I don't know. Or some 3rd-party package
  • @nestjs/common
  • @nestjs/core
  • @nestjs/microservices
  • @nestjs/platform-express
  • @nestjs/platform-fastify
  • @nestjs/platform-socket.io
  • @nestjs/platform-ws
  • @nestjs/testing
  • @nestjs/websockets
  • Other (see below)

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?

  • macOS
  • Windows
  • Linux

Other

No response

@RocheVal RocheVal added the needs triage This issue has not been looked into label Jan 10, 2024
@yannkaiser
Copy link

yannkaiser commented Feb 2, 2024

This is due to the exceptionProxy when you call NestFactory.create, see:

const teardown = this.abortOnError === false ? rethrow : undefined;

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
Copy link
Author

RocheVal commented Feb 5, 2024

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
Copy link
Member

micalevisk commented Jun 21, 2024

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

this.exceptionHandler.handle(e);

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?

@micalevisk micalevisk moved this from 🤔 I'll investigate later to 💭 Maybe I'll take this in My contributions to NestJS framework 😻 Jun 21, 2024
@github-project-automation github-project-automation bot moved this from 💭 Maybe I'll take this to 😔 abandoned in My contributions to NestJS framework 😻 Jul 1, 2024
@RocheVal
Copy link
Author

RocheVal commented Jul 1, 2024

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
Copy link
Member

@RocheVal please open a feature request issue for that

@RocheVal
Copy link
Author

RocheVal commented Jul 1, 2024

Done #13735

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage This issue has not been looked into
Projects
None yet
Development

No branches or pull requests

4 participants