nestjs-sentry icon indicating copy to clipboard operation
nestjs-sentry copied to clipboard

Question: Is there way to access Sentry directly in app.ts

Open sedhuait opened this issue 3 years ago • 0 comments

Hey there, Is there a way to access Sentry in app.ts to capture uncaught exceptions. please find the sample code below

//app.ts

import {
  INestApplication,
  NestApplicationOptions,
  ValidationPipe,
  BadRequestException,
  ValidationError,
  RequestMethod,
} from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { ExpressAdapter } from '@nestjs/platform-express';
import express from 'express';

import { AppModule } from './app.module';
import { ConfigService, isLocal } from './platform';

const ERROR_EXIT_CODE = 1;

/* istanbul ignore next */
process
  .on('unhandledRejection', reason => {
    // eslint-disable-next-line no-console
    console.error(reason);
    Sentry.captureException(reason);
    process.exit(ERROR_EXIT_CODE);
  })
  .on('uncaughtException', err => {
    // eslint-disable-next-line no-console
    console.error(err);
    Sentry.captureException(err);
    process.exit(ERROR_EXIT_CODE);
  });

export default async function createApp(): Promise<INestApplication> {
  const expressServer = express();
  const httpAdapter = new ExpressAdapter(expressServer);
  const options: NestApplicationOptions = { logger: false, bodyParser: false };
  const app: INestApplication = await NestFactory.create(AppModule, httpAdapter, options);
  const config = app.get(ConfigService);

  app.enableCors();
  
  );

  return app;
}

sedhuait avatar Jul 01 '22 13:07 sedhuait