Errors not bubbling up
Is there an existing issue for this?
- [x] I have searched the existing issues
Current behavior
Hello! Recently I was doing some housekeeping in my projects and one of them is using nest-commander.
The project is about a simple CLI to interact with OpenSearch in a convenient way.
When running the CLI will leverage @nestjs/config in order to check a couple of ENV variables. If the variable is expected and not set the code will throw a ConfigException.
As you can see from the attached screenshot I can see the log about the error
but the throw seems somehow ignored
It wont happen in regular nestjs projects
Minimum reproduction code
You can just download and run my project:
-
git clone [email protected]:andreafspeziale/os-cli.git -
cd os-cli -
pnpm i -
pnpm build
At this point all CLI commands should throw since expected env is missing, you can run whatever like
-
node dist/os-cli.js
Expected behavior
The thrown error to be reported
Package
- [x]
nest-commander - [ ]
nest-commander-schematics - [ ]
nest-commander-testing
Package version
3.16.1
Node.js version
v22.14.0
In which operating systems have you tested?
- [x] macOS
- [ ] Windows
- [ ] Linux
Other
You can chat me on X or Discord (added u) to check it out together if needed. Thank you for your help and work 🙏
Hello, I've been digging a little bit more.
Looking at the README, even doing this I'm not able to see my expected errors:
If you need to add in some error handling, there's always try/catch wrapping the run command, or you can chain on some .catch() method to the bootstrap() call.
But following the breadcrumbs and looking into CommandFactory.run it seems that everything is falling apart when hitting this line https://github.com/jmcdo29/nest-commander/blob/75427ff99f28bc8b553a84ec2f279d5233baccc0/packages/nest-commander/src/command.factory.ts#L53
So I'm thinking there might be some regression somewhere... eventually in NestJS v11 itself
@andreafspeziale I ran into the same problem and found this open issue searching for a solution. The docs suggests that wrapping the call in a try/catch or using catch should be enough, but the library doesn't actually bubble the error. After some trial and error, I discovered that explicitly defining a serviceErrorHandler that re-throws the error forces it to bubble up.
await CommandFactory.run(CliAppModule, {
serviceErrorHandler: (error: Error) => {
throw error;
},
});
@jorgeborges Thanks for your message. It has been a while since I opened this issue. Luckily I wrote some nice walkthrough and I quickly cloned back my repo and tried your suggestions plus a couple of alternatives unsuccessfully. Would you mind try it too on your side?
It should be super easy:
-
git clone [email protected]:andreafspeziale/os-cli.git -
cd os-cli -
pnpm i -
pnpm build -
node dist/os-cli.js
I tried:
#!/usr/bin/env node
import { CommandFactory } from 'nest-commander';
import { OSCLIModule } from './os-cli.module';
async function bootstrap(): Promise<void> {
await CommandFactory.run(OSCLIModule, {
serviceErrorHandler: (err) => {
throw err;
},
});
}
bootstrap();
with no luck. I added also some console.log, none was displayed.
Running the cli right away will throw this error https://github.com/andreafspeziale/os-cli/blob/develop/src/config/config.utils.ts#L16, in fact a console.log right before will be displayed, after that none.
Thx in advance and let me know if u got the chance to play with it