nestjs-sentry
nestjs-sentry copied to clipboard
Question: Is there way to access Sentry directly in app.ts
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;
}