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

22 lines
797 B
TypeScript

import { ArgumentsHost, Catch, ExceptionFilter, HttpException, HttpStatus } from '@nestjs/common';
@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 = exception instanceof HttpException ? exception.getStatus() : HttpStatus.INTERNAL_SERVER_ERROR;
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);
}
}