Nest: Allow override of NestFactory.create error handling

Created on 7 Jan 2020  路  6Comments  路  Source: nestjs/nest

Feature Request

Is your feature request related to a problem? Please describe.

Hey! My situation is this:

I want to log startup errors explicitly and log additional information on those errors. I can override default logger, but underneath there is an ExceptionHandler class which will only log occurred errors like this:

ExceptionHandler.logger.error(exception.message, exception.stack);

This means that if a custom error is thrown and it looks something like this:

{
  "message": "Database connection failed"
  "stack": "..............."
  "code": "STARTUP_ERROR"
}

code property will be lost and will not be logged.

Describe the solution you'd like

Solution could be something like this:

const logger = new Logger();

async function bootstrap() {
  const app = await NestFactory.create(AppModule, { logger, throwErrors: true });

  //Additional code here that can throw errors

  await app.listen(3000);
}

bootstrap().catch(error => {
  logger.error(error, null, 'Bootstrap');
  process.exit();
});

throwErrors is false by default and it means that internal ExceptionHandler is used. if value is true - ExceptionHandler is not used and original error is thrown as is or rethrown, allowing explicit handling.

In this case new Logger() is an instance of a custom logger implementing LoggerService of @nestjs/common, and this logger handled error object by whatever logic it deems necessary.

Teachability, Documentation, Adoption, Migration Strategy

Already done in point above :)

What is the motivation / use case for changing the behavior?

I want to log errors that are correctly tagged not only by log level, but by other criteria, or just log additional information that is not part of exception message or stacktrace.

needs triage type

Most helpful comment

UPDATE: mostly settled my issue, logger was off by default...

I've been messing around with https://github.com/Pop-Code/nestjs-console and I'm finding that errors are getting swallowed deep in nestjs code, which ideally could be thrown for quicker debugging. Further details at https://github.com/Pop-Code/nestjs-console/issues/75

Also process.abort at https://github.com/nestjs/nest/blob/a68ae579d4cf6f096708188a861d8a3b76a3268d/packages/core/nest-factory.ts#L166 isn't hitting me in this particular case but in theory it seems like it would hide the error?

All 6 comments

Just create an exception filter https://docs.nestjs.com/exception-filters and use it instead of the default one

@kamilmysliwiec to use a custom exception filter I need something like this:

async function bootstrap() {
  const app = await NestFactory.create(AppModule, { logger });
  app.useGlobalFilters(new HttpExceptionFilter());
  //Additional code here that can throw errors

  await app.listen(3000);
}

If error occurs during or before this line:

const app = await NestFactory.create(AppModule, { logger });

then we never reach the stage where exception filter is usable, and still loose full error context. My case targets logging exceptions which can occur during modules initialization, and exception filters do not handle such cases.

@kamilmysliwiec Can you please reopen this issue or provide some other means of explicitly handling startup errors? I'm not sure if there is anything else I can add to my previous comments, but exception filter does not work in this particular case :)

UPDATE: mostly settled my issue, logger was off by default...

I've been messing around with https://github.com/Pop-Code/nestjs-console and I'm finding that errors are getting swallowed deep in nestjs code, which ideally could be thrown for quicker debugging. Further details at https://github.com/Pop-Code/nestjs-console/issues/75

Also process.abort at https://github.com/nestjs/nest/blob/a68ae579d4cf6f096708188a861d8a3b76a3268d/packages/core/nest-factory.ts#L166 isn't hitting me in this particular case but in theory it seems like it would hide the error?

A solution to this can be to provide an interface NestExceptionsHandler an implementation of which can be passed into the options in nestfactory.create() and that exceptionhandler can be used in place of the default exception handler in exceptions-zone.js
export class ExceptionHandler implements NestExceptionHandler { handle(error) { // your custom error logic} } }
and then
await NestFactory.create(MainModule, {exceptionHandler: new ExceptionHandler()})
and then it can be used in exception-zone.js before calling teardown()
catch (e) { this.exceptionHandler.handle(e); teardown

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cojack picture cojack  路  3Comments

marshall007 picture marshall007  路  3Comments

2233322 picture 2233322  路  3Comments

rlesniak picture rlesniak  路  3Comments

mishelashala picture mishelashala  路  3Comments