feat(exception): 优化错误响应格式。
This commit is contained in:
parent
e908d2981d
commit
bf4590bd4c
@ -1,55 +1,27 @@
|
|||||||
import {
|
import {
|
||||||
ArgumentsHost,
|
|
||||||
Catch,
|
|
||||||
ExceptionFilter,
|
ExceptionFilter,
|
||||||
|
Catch,
|
||||||
|
ArgumentsHost,
|
||||||
HttpException,
|
HttpException,
|
||||||
HttpStatus,
|
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { EntityNotFoundError } from 'typeorm/error/EntityNotFoundError';
|
import { ApolloError } from 'apollo-server-errors';
|
||||||
|
|
||||||
@Catch()
|
@Catch(HttpException)
|
||||||
export class AllExceptionsFilter implements ExceptionFilter {
|
export class HttpExceptionFilter implements ExceptionFilter {
|
||||||
catch(exception: any, host: ArgumentsHost) {
|
catch(exception: HttpException, host: ArgumentsHost) {
|
||||||
const ctx = host.switchToHttp();
|
const message = exception.message;
|
||||||
const response = ctx.getResponse();
|
const extensions: Record<string, any> = {};
|
||||||
const request = ctx.getRequest();
|
const err = exception.getResponse();
|
||||||
|
if (typeof err === 'string') {
|
||||||
const status =
|
extensions.message = err;
|
||||||
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 {
|
} else {
|
||||||
response.status(status).json({
|
Object.assign(extensions, (err as any).extension);
|
||||||
message: ex,
|
extensions.message = (err as any).message;
|
||||||
timestamp: Date.now(),
|
|
||||||
path: request.url,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} else if (exception instanceof EntityNotFoundError) {
|
|
||||||
response.status(HttpStatus.NOT_FOUND).json({
|
|
||||||
message: '资源未找到!',
|
|
||||||
timestamp: Date.now(),
|
|
||||||
path: request.url,
|
|
||||||
});
|
|
||||||
} 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,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
10
src/main.ts
10
src/main.ts
@ -2,11 +2,19 @@ import { ValidationPipe } from '@nestjs/common';
|
|||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
import { NestFactory } from '@nestjs/core';
|
import { NestFactory } from '@nestjs/core';
|
||||||
import { AppModule } from './app.module';
|
import { AppModule } from './app.module';
|
||||||
|
import { HttpExceptionFilter } from './commons/filters/all.exception-filter';
|
||||||
|
import { SanitizePipe } from './commons/pipes/sanitize.pipe';
|
||||||
|
|
||||||
async function bootstrap() {
|
async function bootstrap() {
|
||||||
const app = await NestFactory.create(AppModule);
|
const app = await NestFactory.create(AppModule);
|
||||||
const configService = app.get(ConfigService);
|
const configService = app.get(ConfigService);
|
||||||
app.useGlobalPipes(new ValidationPipe());
|
app.useGlobalPipes(new SanitizePipe());
|
||||||
|
app.useGlobalPipes(
|
||||||
|
new ValidationPipe({
|
||||||
|
transform: true,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
app.useGlobalFilters(new HttpExceptionFilter());
|
||||||
await app.listen(configService.get<number>('http.port'));
|
await app.listen(configService.get<number>('http.port'));
|
||||||
}
|
}
|
||||||
bootstrap();
|
bootstrap();
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { InputType } from '@nestjs/graphql';
|
import { InputType } from '@nestjs/graphql';
|
||||||
import { WorkUnitMetadata } from '../../pipeline-tasks/models/work-unit-metadata.model';
|
import { WorkUnitMetadata } from '../../pipeline-tasks/models/work-unit-metadata.model';
|
||||||
import {
|
import {
|
||||||
IsInstance,
|
IsObject,
|
||||||
IsOptional,
|
IsOptional,
|
||||||
IsString,
|
IsString,
|
||||||
IsUUID,
|
IsUUID,
|
||||||
@ -22,6 +22,6 @@ export class CreatePipelineInput {
|
|||||||
name: string;
|
name: string;
|
||||||
|
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@IsInstance(WorkUnitMetadata)
|
@IsObject()
|
||||||
workUnitMetadata: WorkUnitMetadata;
|
workUnitMetadata: WorkUnitMetadata;
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,9 @@ import { ListPipelineArgs } from './dtos/list-pipelines.args';
|
|||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class PipelinesService extends BaseDbService<Pipeline> {
|
export class PipelinesService extends BaseDbService<Pipeline> {
|
||||||
readonly uniqueFields: Array<keyof Pipeline> = ['branch', 'projectId'];
|
readonly uniqueFields: Array<Array<keyof Pipeline>> = [
|
||||||
|
['branch', 'projectId'],
|
||||||
|
];
|
||||||
constructor(
|
constructor(
|
||||||
@InjectRepository(Pipeline)
|
@InjectRepository(Pipeline)
|
||||||
readonly repository: Repository<Pipeline>,
|
readonly repository: Repository<Pipeline>,
|
||||||
|
Loading…
Reference in New Issue
Block a user