think/packages/server/src/exceptions/http-response.exception.ts

23 lines
797 B
TypeScript
Raw Normal View History

2022-03-12 03:27:56 +00:00
import { ArgumentsHost, Catch, ExceptionFilter, HttpException, HttpStatus } from '@nestjs/common';
2022-02-20 11:51:55 +00:00
@Catch(HttpException)
export class HttpResponseExceptionFilter implements ExceptionFilter {
catch(exception: HttpException, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse();
const request = ctx.getRequest();
const url = request.originalUrl;
const status =
2022-03-12 03:27:56 +00:00
exception instanceof HttpException ? exception.getStatus() : HttpStatus.INTERNAL_SERVER_ERROR;
2022-02-20 11:51:55 +00:00
const errorResponse = {
statusCode: status,
message: exception.message,
success: false,
data: null,
};
response.status(status);
response.header('Content-Type', 'application/json; charset=utf-8');
response.send(errorResponse);
}
}