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

App Crashing on Bot Error #1244

Open
llegoelkelo opened this issue Aug 25, 2024 · 1 comment
Open

App Crashing on Bot Error #1244

llegoelkelo opened this issue Aug 25, 2024 · 1 comment

Comments

@llegoelkelo
Copy link

Relates to #1156

I find my app crashing whenever the bot throws. Is there a way to prevent this from happening?

@vaagnavanesyan
Copy link

@llegoelkelo you can handle all unhandled Promise rejections (not only from this library) by adding listener for unhandledRejection events.

The easiest way is to add following code before await this.app.listen(port);:

process.on('unhandledRejection', (reason) => {
      console.error(reason);
    });

If you prefer more robust way (e.g. sending logs or any other action in case of error) you can achieve that by two following steps:

  1. Declare class-wrapper for unhandledRejection handler:
import { Injectable, Logger } from '@nestjs/common';

@Injectable()
export class UnhandledLogger {
  private readonly logger: Logger = new Logger(UnhandledLogger.name);
  constructor() {
    process.on('unhandledRejection', (reason) => {
      this.logger.error(reason);
      // here you can do anything else
    });
  }
}
  1. Add that wrapper to AppModule providers array:
providers: [
    UnhandledLogger,
    {
      provide: APP_GUARD,
      useClass: AuthenticationGuard,
    },
  ],

Second approach is more flexible because UnhandledLogger is simple NestJS Service so you can use IoC, DB connections and anything else.

To test this solution you can simply run two instances of your NestJS application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants