Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/exception.filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {
ExceptionFilter,
Catch,
ArgumentsHost,
Logger,
HttpStatus,
} from '@nestjs/common';
import {Request, Response} from 'express';

@Catch(Error)
export class HttpExceptionFilter implements ExceptionFilter {
private readonly logger = new Logger(HttpExceptionFilter.name);
public catch(exception: Error, host: ArgumentsHost): void {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
const request = ctx.getRequest<Request>();
const status = HttpStatus.INTERNAL_SERVER_ERROR;

this.logger.error(exception);

response.status(status).json({
statusCode: status,
timestamp: new Date().toISOString(),
path: request.url,
});
}
}
8 changes: 7 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ import {DocumentBuilder, SwaggerModule} from '@nestjs/swagger';
import {useContainer} from 'class-validator';

import {AppModule} from '@warp-core/app.module';
import {HttpExceptionFilter} from '@warp-core/exception.filter';

async function bootstrap(): Promise<void> {
const appURL = '/graphql';
const localDocUrl = '/doc';
const app = await NestFactory.create(AppModule);
const app = await NestFactory.create(AppModule, {
cors: {
origin: false,
},
});
app.useGlobalFilters(new HttpExceptionFilter());
app.useGlobalPipes(new ValidationPipe());

useContainer(app.select(AppModule), {fallbackOnErrors: true});
Expand Down
Loading