mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-18 05:09:29 +00:00
add auth
This commit is contained in:
37
src/exception.filter.ts
Normal file
37
src/exception.filter.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
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();
|
||||
if (status === 400) {
|
||||
const errorsResponse = {
|
||||
errorsMessages: [],
|
||||
};
|
||||
const responseBody: any = exception.getResponse();
|
||||
if (typeof responseBody.message === 'object') {
|
||||
responseBody.message.forEach((e) =>
|
||||
errorsResponse.errorsMessages.push(e),
|
||||
);
|
||||
} else {
|
||||
errorsResponse.errorsMessages.push(responseBody.message);
|
||||
}
|
||||
response.status(status).json(errorsResponse);
|
||||
} else {
|
||||
response.status(status).json({
|
||||
statusCode: status,
|
||||
timestamp: new Date().toISOString(),
|
||||
path: request.url,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user