feat: Basic setup with auth
This commit is contained in:
8
common/exceptions/custom-exceptions.ts
Normal file
8
common/exceptions/custom-exceptions.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { HttpException, HttpStatus } from '@nestjs/common';
|
||||
|
||||
// Base exception
|
||||
export class BaseException extends HttpException {
|
||||
protected constructor(code: string, message: string, status: HttpStatus) {
|
||||
super({ code, message }, status);
|
||||
}
|
||||
}
|
||||
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,
|
||||
});
|
||||
}
|
||||
}
|
||||
1
common/exceptions/index.ts
Normal file
1
common/exceptions/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './custom-exceptions';
|
||||
Reference in New Issue
Block a user