Skip to content
Snippets Groups Projects
Commit 60b991dc authored by Etienne LOUPIAS's avatar Etienne LOUPIAS
Browse files

wip exception handler

parent 145d0da8
No related branches found
No related tags found
No related merge requests found
......@@ -2,6 +2,7 @@ import { ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { AppModule } from './app.module';
import { MailerService } from './mailer/mailer.service';
async function bootstrap() {
const app = await NestFactory.create(AppModule, {
......@@ -9,6 +10,44 @@ async function bootstrap() {
process.env.NODE_ENV === 'production' ? ['error', 'warn', 'log'] : ['log', 'debug', 'error', 'verbose', 'warn'],
});
app.useGlobalPipes(new ValidationPipe());
//const exceptionService = app.get(ExceptionService);
/*
const mailerService = app.get(MailerService);
process.on('uncaughtException', async (err) => {
//exceptionService.handleUncaughtException(err);
console.log('Uncaught Exception:', err);
try {
await mailerService.send(
'to_complete@xxx.com',
'Uncaught Exception in NestJS Application',
`An uncaught exception occurred: <br />${err.name} : ${err.message}<br />${err.stack}`
);
} catch (e) {
console.log('Uncaught Exception process failed:', e);
}
// Exit the application on an uncaught exception to maintain stability and facilitate debugging
process.exit(1);
});
process.on('unhandledRejection', async (reason, promise) => {
console.log('Unhandled rejection at ', promise, `reason: ${reason}`);
try {
await mailerService.send(
'to_complete@xxx.com',
'Unhandled rejection in NestJS Application',
`An unhandled rejection occurred: <br />${JSON.stringify(promise)}<br />reason: ${reason}`
);
} catch (e) {
console.log('Unhandled rejection process failed:', e);
}
// Exit the application on an unhandled rejection to maintain stability and facilitate debugging
process.exit(1);
});
*/
const options = new DocumentBuilder()
.setTitle(`Res'in`)
.setDescription(`Res'in API documentation`)
......@@ -18,6 +57,7 @@ async function bootstrap() {
app.setGlobalPrefix('api');
const document = SwaggerModule.createDocument(app, options);
SwaggerModule.setup('api/doc', app, document);
await app.listen(3000);
}
bootstrap();
import { Injectable } from '@nestjs/common';
import { MailerService } from '../mailer/mailer.service';
@Injectable()
export class ExceptionService {
constructor(private readonly mailerService: MailerService) {}
handleUncaughtException(err: Error) {
console.log('Uncaught Exception :', err);
this.mailerService.send(
'eloupias@grandlyon.com',
'Uncaught Exception in NestJS Application',
`An uncaught exception occurred: ${JSON.stringify(err)}`
);
}
handleUnhandledRejection(reason: any) {
console.log('Unhandled Rejection:', reason);
this.mailerService.send(
'eloupias@grandlyon.com',
'Unhandled Promise Rejection in NestJS Application',
`An unhandled promise rejection occurred: ${reason}`
);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment