From ec351d12f290de7b1e522539939d2ab23635e619 Mon Sep 17 00:00:00 2001 From: Ivan Li Date: Sat, 10 Jul 2021 18:50:01 +0800 Subject: [PATCH] =?UTF-8?q?fix(pipelines):=20=E4=BB=BB=E5=8A=A1=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=E4=B8=8D=E5=BE=97=E4=B8=BA=E7=A9=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/commons/filters/all.exception-filter.ts | 11 +++++++++-- src/main.ts | 4 +++- src/pipeline-tasks/models/work-unit-metadata.model.ts | 6 ++++++ src/pipeline-tasks/models/work-unit.model.ts | 3 +++ src/pipelines/dtos/create-pipeline.input.ts | 8 ++++++-- 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/commons/filters/all.exception-filter.ts b/src/commons/filters/all.exception-filter.ts index d777db6..7daaefa 100644 --- a/src/commons/filters/all.exception-filter.ts +++ b/src/commons/filters/all.exception-filter.ts @@ -6,13 +6,18 @@ import { HttpStatus, } from '@nestjs/common'; import { ApolloError } from 'apollo-server-errors'; +import { PinoLogger, InjectPinoLogger } from 'nestjs-pino'; @Catch(HttpException) export class HttpExceptionFilter implements ExceptionFilter { + constructor( + @InjectPinoLogger(HttpExceptionFilter.name) + private readonly logger: PinoLogger, + ) {} catch(exception: HttpException, host: ArgumentsHost) { switch (host.getType<'http' | 'graphql' | string>()) { case 'graphql': { - const message = exception.message; + const errorName = exception.message; const extensions: Record = {}; const err = exception.getResponse(); if (typeof err === 'string') { @@ -21,8 +26,10 @@ export class HttpExceptionFilter implements ExceptionFilter { Object.assign(extensions, (err as any).extension); extensions.message = (err as any).message; } + extensions.error = errorName; + this.logger.error(extensions); return new ApolloError( - message, + extensions.message, exception.getStatus().toString(), extensions, ); diff --git a/src/main.ts b/src/main.ts index ed4d24d..1b9d634 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,3 +1,4 @@ +import { PinoLogger } from 'nestjs-pino'; import { ValidationPipe } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { NestFactory } from '@nestjs/core'; @@ -14,7 +15,8 @@ async function bootstrap() { transform: true, }), ); - app.useGlobalFilters(new HttpExceptionFilter()); + const httpExceptionFilterLogger = await app.resolve(PinoLogger); + app.useGlobalFilters(new HttpExceptionFilter(httpExceptionFilterLogger)); await app.listen(configService.get('http.port')); } bootstrap(); diff --git a/src/pipeline-tasks/models/work-unit-metadata.model.ts b/src/pipeline-tasks/models/work-unit-metadata.model.ts index c95afaf..b884fc3 100644 --- a/src/pipeline-tasks/models/work-unit-metadata.model.ts +++ b/src/pipeline-tasks/models/work-unit-metadata.model.ts @@ -1,4 +1,6 @@ import { Field, InputType, Int, ObjectType } from '@nestjs/graphql'; +import { Type } from 'class-transformer'; +import { IsInstance, isInstance, ValidateNested } from 'class-validator'; import { WorkUnit } from './work-unit.model'; @InputType('WorkUnitMetadataInput') @@ -6,5 +8,9 @@ import { WorkUnit } from './work-unit.model'; export class WorkUnitMetadata { @Field(() => Int) version = 1; + + @Type(() => WorkUnit) + @IsInstance(WorkUnit, { each: true }) + @ValidateNested({ each: true }) units: WorkUnit[]; } diff --git a/src/pipeline-tasks/models/work-unit.model.ts b/src/pipeline-tasks/models/work-unit.model.ts index 08191f0..8a00e4f 100644 --- a/src/pipeline-tasks/models/work-unit.model.ts +++ b/src/pipeline-tasks/models/work-unit.model.ts @@ -1,4 +1,5 @@ import { Field, InputType, ObjectType } from '@nestjs/graphql'; +import { IsNotEmpty } from 'class-validator'; import { PipelineUnits, PipelineUnits as PipelineUnitTypes, @@ -9,5 +10,7 @@ import { export class WorkUnit { @Field(() => PipelineUnits) type: PipelineUnitTypes; + + @IsNotEmpty({ each: true }) scripts: string[]; } diff --git a/src/pipelines/dtos/create-pipeline.input.ts b/src/pipelines/dtos/create-pipeline.input.ts index e72c63c..37e2870 100644 --- a/src/pipelines/dtos/create-pipeline.input.ts +++ b/src/pipelines/dtos/create-pipeline.input.ts @@ -1,11 +1,13 @@ +import { Type } from 'class-transformer'; import { InputType } from '@nestjs/graphql'; import { WorkUnitMetadata } from '../../pipeline-tasks/models/work-unit-metadata.model'; import { - IsObject, + IsInstance, IsOptional, IsString, IsUUID, MaxLength, + ValidateNested, } from 'class-validator'; @InputType({ isAbstract: true }) @@ -21,7 +23,9 @@ export class CreatePipelineInput { @MaxLength(32) name: string; + @Type(() => WorkUnitMetadata) @IsOptional() - @IsObject() + @ValidateNested() + @IsInstance(WorkUnitMetadata) workUnitMetadata: WorkUnitMetadata; }