Skip to content

Commit

Permalink
Throw errors instead of returning
Browse files Browse the repository at this point in the history
  • Loading branch information
thomtrp committed Aug 14, 2024
1 parent e4ed6e1 commit b80e83b
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class WorkflowRunWorkspaceEntity extends BaseWorkspaceEntity {

@WorkspaceField({
standardId: WORKFLOW_RUN_STANDARD_FIELD_IDS.status,
type: FieldMetadataType.TEXT,
type: FieldMetadataType.SELECT,
label: 'Workflow run status',
description: 'Workflow run status',
icon: 'IconHistory',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { CustomException } from 'src/utils/custom-exception';

export class WorkflowRunnerException extends CustomException {
constructor(message: string, code: string) {
super(message, code);
}
}

export enum WorkflowRunnerExceptionCode {
WORKFLOW_FAILED = 'WORKFLOW_FAILED',
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,24 @@ export class WorkflowRunnerJob {
workflowVersionId,
);

const output = await this.workflowRunnerService.run({
action: workflowVersion.trigger.nextAction,
workspaceId,
payload,
});
try {
await this.workflowRunnerService.run({
action: workflowVersion.trigger.nextAction,
workspaceId,
payload,
});

await this.workflowStatusWorkspaceService.endWorkflowRun(
workflowRunId,
output.error ? WorkflowRunStatus.FAILED : WorkflowRunStatus.COMPLETED,
);
await this.workflowStatusWorkspaceService.endWorkflowRun(
workflowRunId,
WorkflowRunStatus.COMPLETED,
);
} catch (error) {
await this.workflowStatusWorkspaceService.endWorkflowRun(
workflowRunId,
WorkflowRunStatus.FAILED,
);

throw error;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { Injectable } from '@nestjs/common';

import { WorkflowAction } from 'src/modules/workflow/common/types/workflow-action.type';
import { WorkflowActionRunnerFactory } from 'src/modules/workflow/workflow-action-runner/workflow-action-runner.factory';
import {
WorkflowRunnerException,
WorkflowRunnerExceptionCode,
} from 'src/modules/workflow/workflow-runner/workflow-runner.exception';

const MAX_RETRIES_ON_FAILURE = 3;

Expand Down Expand Up @@ -52,7 +56,10 @@ export class WorkflowRunnerService {
}

if (!result.error) {
throw new Error('Execution result error, no data or error');
throw new WorkflowRunnerException(
'Execution result error, no data or error',
WorkflowRunnerExceptionCode.WORKFLOW_FAILED,
);
}

if (action.settings.errorHandlingOptions.continueOnFailure.value) {
Expand All @@ -75,8 +82,9 @@ export class WorkflowRunnerService {
});
}

return {
error: result.error,
};
throw new WorkflowRunnerException(
`Workflow failed: ${result.error}`,
WorkflowRunnerExceptionCode.WORKFLOW_FAILED,
);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { Module } from '@nestjs/common';

import { TwentyORMModule } from 'src/engine/twenty-orm/twenty-orm.module';
import { WorkflowRunWorkspaceEntity } from 'src/modules/workflow/common/standard-objects/workflow-run.workspace-entity';
import { WorkflowStatusWorkspaceService } from 'src/modules/workflow/workflow-status/workflow-status.workspace-service';

@Module({
imports: [TwentyORMModule.forFeature([WorkflowRunWorkspaceEntity])],
providers: [WorkflowStatusWorkspaceService],
exports: [WorkflowStatusWorkspaceService],
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { Module } from '@nestjs/common';

import { TwentyORMModule } from 'src/engine/twenty-orm/twenty-orm.module';
import { WorkflowWorkspaceEntity } from 'src/modules/workflow/common/standard-objects/workflow.workspace-entity';
import { WorkflowRunnerModule } from 'src/modules/workflow/workflow-runner/workflow-runner.module';
import { WorkflowStatusModule } from 'src/modules/workflow/workflow-status/workflow-status.module';
import { WorkflowEventTriggerJob } from 'src/modules/workflow/workflow-trigger/jobs/workflow-event-trigger.job';

@Module({
imports: [
WorkflowRunnerModule,
WorkflowStatusModule,
TwentyORMModule.forFeature([WorkflowWorkspaceEntity]),
],
imports: [WorkflowRunnerModule, WorkflowStatusModule],
providers: [WorkflowEventTriggerJob],
})
export class WorkflowTriggerJobModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ export enum WorkflowTriggerExceptionCode {
INVALID_WORKFLOW_TRIGGER = 'INVALID_WORKFLOW_TRIGGER',
INVALID_WORKFLOW_VERSION = 'INVALID_WORKFLOW_VERSION',
INVALID_ACTION_TYPE = 'INVALID_ACTION_TYPE',
INTERNAL_ERROR = 'INTERNAL_ERROR',
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,18 @@ export class WorkflowTriggerService {
workflowVersionId,
);

return await this.workflowRunnerService.run({
action: workflowVersion.trigger.nextAction,
workspaceId,
payload,
});
try {
return await this.workflowRunnerService.run({
action: workflowVersion.trigger.nextAction,
workspaceId,
payload,
});
} catch (error) {
throw new WorkflowTriggerException(
`Error running workflow version ${error}`,
WorkflowTriggerExceptionCode.INTERNAL_ERROR,
);
}
}

async enableWorkflowTrigger(workspaceId: string, workflowVersionId: string) {
Expand Down

0 comments on commit b80e83b

Please sign in to comment.