feat: Basic setup with auth
This commit is contained in:
32
common/exceptions/exception-filter.ts
Normal file
32
common/exceptions/exception-filter.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import {
|
||||
ExceptionFilter,
|
||||
Catch,
|
||||
ArgumentsHost,
|
||||
HttpException,
|
||||
Logger,
|
||||
} from '@nestjs/common';
|
||||
import { Request, Response } from 'express';
|
||||
|
||||
@Catch(HttpException) // What exception to catch
|
||||
export class HttpExceptionFilter implements ExceptionFilter<HttpException> {
|
||||
constructor(private readonly logger: Logger) {}
|
||||
catch(exception: HttpException, host: ArgumentsHost) {
|
||||
const ctx = host.switchToHttp();
|
||||
const request: Request = ctx.getRequest();
|
||||
const response: Response = ctx.getResponse();
|
||||
const status = exception.getStatus();
|
||||
|
||||
if (status >= 500) {
|
||||
this.logger.warn({
|
||||
method: request.method,
|
||||
url: request.url,
|
||||
message: exception.message,
|
||||
});
|
||||
}
|
||||
|
||||
response.status(status).json({
|
||||
message: exception.message,
|
||||
statusCode: status,
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user