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

22 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();
2022-05-16 09:23:59 +00:00
// const request = ctx.getRequest();
// const url = request.originalUrl;
2022-03-22 05:37:53 +00:00
const status = 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);
}
}