jmcdo29 / nest-commander

A module for using NestJS to build up CLI applications
https://nest-commander.jaymcdoniel.dev/
MIT License
435 stars 53 forks source link

Errors are not logged through custom logger #1121

Open coler-j opened 6 months ago

coler-j commented 6 months ago

Is there an existing issue for this?

Current behavior

When an error is raised

  1. I am not recieving it within my errorHandler
  2. The error output is not using the custom logger I have injected following https://github.com/jmcdo29/nest-commander/issues/852

My output will be like:

Error: TEST[22:09:29.800] INFO (21106): Starting Nest application...
    context: "NestFactory"
[22:09:29.800] INFO (21106): CommandRootModule dependencies initialized
    context: "InstanceLoader"
[22:09:29.800] INFO (21106): AppModule dependencies initialized
    context: "InstanceLoader"
[22:09:29.800] INFO (21106): InstrumentationModule dependencies initialized
    context: "InstanceLoader"
[22:09:29.800] INFO (21106): OpenTelemetryModule dependencies initialized
    context: "InstanceLoader"
..... more logs

The output starts with the error Error: TEST which is not formatted as it should be with my logger, and then the buffered logs continue afterwards...

Minimum reproduction code

Command that returns an error

import { Logger } from '@nestjs/common';
import { Command, CommandRunner } from 'nest-commander';

@Command({
  name: 'test', 
})
export class SomeCommand extends CommandRunner {
  private readonly logger = new Logger(SomeCommand.name);

  async run(inputs: string[], options: Record<string, unknown>): Promise<void> {
    this.logger.log(inputs);
    this.logger.log(options);
    await Promise.resolve();
    throw new Error('TEST');
  }
}

Expected behavior

Error is written with custom logger

Package

Package version

3.12.5

Node.js version

20.11.1

In which operating systems have you tested?

Other

No response

coler-j commented 6 months ago

What is the difference between serviceErrorHandler and errorHandler

jmcdo29 commented 6 months ago

serviceErrorHandler was created to handle errors that happen during this.commander.parseAsync as detailed in this PR, whereas errorHandler is the general commander error handler and modifies how errors at the commander level are handled