From 36936e2f6b31a1e5f04ea2dc3de6dbfe6e7ec775 Mon Sep 17 00:00:00 2001 From: Thomas Trompette Date: Tue, 20 Aug 2024 14:49:37 +0200 Subject: [PATCH] Use filter to handle exception --- ...w-trigger-graphql-api-exception.filter.ts} | 19 +++++++----- .../workflow/workflow-trigger.resolver.ts | 29 +++++++------------ 2 files changed, 22 insertions(+), 26 deletions(-) rename packages/twenty-server/src/engine/core-modules/workflow/{utils/workflow-trigger-graphql-api-exception-handler.util.ts => filters/workflow-trigger-graphql-api-exception.filter.ts} (59%) diff --git a/packages/twenty-server/src/engine/core-modules/workflow/utils/workflow-trigger-graphql-api-exception-handler.util.ts b/packages/twenty-server/src/engine/core-modules/workflow/filters/workflow-trigger-graphql-api-exception.filter.ts similarity index 59% rename from packages/twenty-server/src/engine/core-modules/workflow/utils/workflow-trigger-graphql-api-exception-handler.util.ts rename to packages/twenty-server/src/engine/core-modules/workflow/filters/workflow-trigger-graphql-api-exception.filter.ts index e3ac10a136c5..13edf52a99ee 100644 --- a/packages/twenty-server/src/engine/core-modules/workflow/utils/workflow-trigger-graphql-api-exception-handler.util.ts +++ b/packages/twenty-server/src/engine/core-modules/workflow/filters/workflow-trigger-graphql-api-exception.filter.ts @@ -1,3 +1,5 @@ +import { Catch, ExceptionFilter } from '@nestjs/common'; + import { InternalServerError, UserInputError, @@ -7,18 +9,19 @@ import { WorkflowTriggerExceptionCode, } from 'src/modules/workflow/workflow-trigger/workflow-trigger.exception'; -export const workflowTriggerGraphqlApiExceptionHandler = (error: Error) => { - if (error instanceof WorkflowTriggerException) { - switch (error.code) { +@Catch(WorkflowTriggerException) +export class WorkflowTriggerGraphqlApiExceptionFilter + implements ExceptionFilter +{ + catch(exception: WorkflowTriggerException) { + switch (exception.code) { case WorkflowTriggerExceptionCode.INVALID_INPUT: - throw new UserInputError(error.message); + throw new UserInputError(exception.message); case WorkflowTriggerExceptionCode.INVALID_WORKFLOW_TRIGGER: case WorkflowTriggerExceptionCode.INVALID_WORKFLOW_VERSION: case WorkflowTriggerExceptionCode.INVALID_ACTION_TYPE: default: - throw new InternalServerError(error.message); + throw new InternalServerError(exception.message); } } - - throw error; -}; +} diff --git a/packages/twenty-server/src/engine/core-modules/workflow/workflow-trigger.resolver.ts b/packages/twenty-server/src/engine/core-modules/workflow/workflow-trigger.resolver.ts index aef50563588f..5d38b2ced392 100644 --- a/packages/twenty-server/src/engine/core-modules/workflow/workflow-trigger.resolver.ts +++ b/packages/twenty-server/src/engine/core-modules/workflow/workflow-trigger.resolver.ts @@ -1,14 +1,15 @@ -import { UseGuards } from '@nestjs/common'; +import { UseFilters, UseGuards } from '@nestjs/common'; import { Args, Mutation, Resolver } from '@nestjs/graphql'; import { RunWorkflowVersionInput } from 'src/engine/core-modules/workflow/dtos/run-workflow-version-input.dto'; import { WorkflowRunDTO } from 'src/engine/core-modules/workflow/dtos/workflow-run.dto'; -import { workflowTriggerGraphqlApiExceptionHandler } from 'src/engine/core-modules/workflow/utils/workflow-trigger-graphql-api-exception-handler.util'; +import { WorkflowTriggerGraphqlApiExceptionFilter } from 'src/engine/core-modules/workflow/filters/workflow-trigger-graphql-api-exception.filter'; import { JwtAuthGuard } from 'src/engine/guards/jwt.auth.guard'; import { WorkflowTriggerWorkspaceService } from 'src/modules/workflow/workflow-trigger/services/workflow-trigger.workspace-service'; -@UseGuards(JwtAuthGuard) @Resolver() +@UseGuards(JwtAuthGuard) +@UseFilters(WorkflowTriggerGraphqlApiExceptionFilter) export class WorkflowTriggerResolver { constructor( private readonly workflowTriggerWorkspaceService: WorkflowTriggerWorkspaceService, @@ -18,26 +19,18 @@ export class WorkflowTriggerResolver { async enableWorkflowTrigger( @Args('workflowVersionId') workflowVersionId: string, ) { - try { - return await this.workflowTriggerWorkspaceService.enableWorkflowTrigger( - workflowVersionId, - ); - } catch (error) { - workflowTriggerGraphqlApiExceptionHandler(error); - } + return await this.workflowTriggerWorkspaceService.enableWorkflowTrigger( + workflowVersionId, + ); } @Mutation(() => WorkflowRunDTO) async runWorkflowVersion( @Args('input') { workflowVersionId, payload }: RunWorkflowVersionInput, ) { - try { - return await this.workflowTriggerWorkspaceService.runWorkflowVersion( - workflowVersionId, - payload ?? {}, - ); - } catch (error) { - workflowTriggerGraphqlApiExceptionHandler(error); - } + return await this.workflowTriggerWorkspaceService.runWorkflowVersion( + workflowVersionId, + payload ?? {}, + ); } }