mirror of https://github.com/fantasticit/think.git
31 lines
824 B
TypeScript
31 lines
824 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);
|
||
|
}
|
||
|
}
|