Skip to content

Commit

Permalink
Use filter to handle exception
Browse files Browse the repository at this point in the history
  • Loading branch information
thomtrp committed Aug 20, 2024
1 parent 2a2dda9 commit 36936e2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Catch, ExceptionFilter } from '@nestjs/common';

import {
InternalServerError,
UserInputError,
Expand All @@ -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;
};
}
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 ?? {},
);
}
}

0 comments on commit 36936e2

Please sign in to comment.