feat(exception): 优化错误响应格式。
This commit is contained in:
@ -1,55 +1,27 @@
|
||||
import {
|
||||
ArgumentsHost,
|
||||
Catch,
|
||||
ExceptionFilter,
|
||||
Catch,
|
||||
ArgumentsHost,
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
} from '@nestjs/common';
|
||||
import { EntityNotFoundError } from 'typeorm/error/EntityNotFoundError';
|
||||
import { ApolloError } from 'apollo-server-errors';
|
||||
|
||||
@Catch()
|
||||
export class AllExceptionsFilter implements ExceptionFilter {
|
||||
catch(exception: any, host: ArgumentsHost) {
|
||||
const ctx = host.switchToHttp();
|
||||
const response = ctx.getResponse();
|
||||
const request = ctx.getRequest();
|
||||
|
||||
const status =
|
||||
exception instanceof HttpException
|
||||
? exception.getStatus()
|
||||
: HttpStatus.INTERNAL_SERVER_ERROR;
|
||||
|
||||
if (exception instanceof HttpException) {
|
||||
const ex = exception.getResponse();
|
||||
if (ex instanceof Object) {
|
||||
response.status(status).json({
|
||||
...ex,
|
||||
timestamp: Date.now(),
|
||||
path: request.url,
|
||||
});
|
||||
} else {
|
||||
response.status(status).json({
|
||||
message: ex,
|
||||
timestamp: Date.now(),
|
||||
path: request.url,
|
||||
});
|
||||
}
|
||||
} else if (exception instanceof EntityNotFoundError) {
|
||||
response.status(HttpStatus.NOT_FOUND).json({
|
||||
message: '资源未找到!',
|
||||
timestamp: Date.now(),
|
||||
path: request.url,
|
||||
});
|
||||
@Catch(HttpException)
|
||||
export class HttpExceptionFilter implements ExceptionFilter {
|
||||
catch(exception: HttpException, host: ArgumentsHost) {
|
||||
const message = exception.message;
|
||||
const extensions: Record<string, any> = {};
|
||||
const err = exception.getResponse();
|
||||
if (typeof err === 'string') {
|
||||
extensions.message = err;
|
||||
} else {
|
||||
console.error('服务器内部错误');
|
||||
console.error(exception);
|
||||
response.status(HttpStatus.INTERNAL_SERVER_ERROR).json({
|
||||
code: status,
|
||||
timestamp: new Date().toISOString(),
|
||||
message: '服务器内部错误',
|
||||
error: exception,
|
||||
path: request.url,
|
||||
});
|
||||
Object.assign(extensions, (err as any).extension);
|
||||
extensions.message = (err as any).message;
|
||||
}
|
||||
return new ApolloError(
|
||||
message,
|
||||
exception.getStatus().toString(),
|
||||
extensions,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
15
src/commons/pipes/sanitize.pipe.ts
Normal file
15
src/commons/pipes/sanitize.pipe.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import { ArgumentMetadata, Injectable, PipeTransform } from '@nestjs/common';
|
||||
import { sanitize } from '@neuralegion/class-sanitizer/dist';
|
||||
|
||||
@Injectable()
|
||||
export class SanitizePipe implements PipeTransform {
|
||||
transform(value: any, metadata: ArgumentMetadata) {
|
||||
// console.log(value, typeof value);
|
||||
if (value instanceof Object) {
|
||||
value = Object.assign(new metadata.metatype(), value);
|
||||
sanitize(value);
|
||||
// console.log(value);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user