然后把异常定义一下

This commit is contained in:
Jeremy Yin 2019-06-18 22:24:11 +08:00
parent 7aea5a1308
commit 0e349316f8
1 changed files with 13 additions and 3 deletions

View File

@ -1,6 +1,16 @@
import { ArgumentsHost, Catch, ExceptionFilter } from '@nestjs/common'; import { ArgumentsHost, Catch, ExceptionFilter, HttpException } from '@nestjs/common';
@Catch() @Catch(HttpException)
export class DemoFilter<T> implements ExceptionFilter { export class DemoFilter<T> implements ExceptionFilter {
catch(exception: T, host: ArgumentsHost) {} catch(exception: HttpException, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse();
const request = ctx.getRequest();
const status = exception.getStatus();
response.status(status).json({
statusCode: status,
path: request.url,
});
}
} }