Skip to content

Commit

Permalink
feat(http): created http exception filter
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasexplore committed Jun 19, 2022
1 parent 46b3124 commit 36312d5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/modules/http/http-exception.filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {
ExceptionFilter,
Catch,
ArgumentsHost,
HttpException,
} from '@nestjs/common';
import { Request, Response } from 'express';

@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter {
catch(exception: HttpException, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
const request = ctx.getRequest<Request>();
const status = exception.getStatus();

response.status(status).json({
error: exception.message,
statusCode: status,
timestamp: new Date().toISOString(),
path: request.url,
});
}
}
1 change: 1 addition & 0 deletions src/modules/http/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './http-exception.filter';

0 comments on commit 36312d5

Please sign in to comment.