Skip to content

Commit

Permalink
feat(core): Add a warning to error workflows that cannot be started d…
Browse files Browse the repository at this point in the history
…ue to permission or settings (#6961)

Github issue / Community forum post (link here to close automatically):

This PR aims to address an issue where an Error workflow cannot be
started, either due to insufficient permissions or because its settings
prevent it from being called.

The way of addressing this is by creating a failed execution for the
appointed error workflow stating the error, as can be seen below.

This means the execution itself won't start, as it's prevented before
the execution beings, but we save a "stub" execution to show the error.

![Screenshot 2023-08-17 at 16 17
02](https://github.com/n8n-io/n8n/assets/219272/d8ec0144-13c5-4b11-b91c-a6b440816ccf)
  • Loading branch information
krynble authored Aug 22, 2023
1 parent c188b0e commit 67b88f7
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 56 deletions.
126 changes: 79 additions & 47 deletions packages/cli/src/WorkflowHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { FindOptionsWhere } from 'typeorm';
import { In } from 'typeorm';
import { Container } from 'typedi';

import type {
IDataObject,
IExecuteData,
Expand All @@ -11,17 +12,20 @@ import type {
ITaskData,
NodeApiError,
WorkflowExecuteMode,
WorkflowOperationError,
} from 'n8n-workflow';
import {
ErrorReporterProxy as ErrorReporter,
LoggerProxy as Logger,
NodeOperationError,
SubworkflowOperationError,
Workflow,
} from 'n8n-workflow';
import { v4 as uuid } from 'uuid';
import * as Db from '@/Db';
import type {
ICredentialsDb,
IExecutionDb,
IWorkflowErrorData,
IWorkflowExecutionDataProcess,
} from '@/Interfaces';
Expand All @@ -39,11 +43,57 @@ import { UserService } from './user/user.service';
import type { SharedWorkflow } from '@db/entities/SharedWorkflow';
import type { RoleNames } from '@db/entities/Role';
import { RoleService } from './services/role.service';
import { RoleRepository } from './databases/repositories';
import { ExecutionRepository, RoleRepository } from './databases/repositories';
import { VariablesService } from './environments/variables/variables.service';

const ERROR_TRIGGER_TYPE = config.getEnv('nodes.errorTriggerType');

export function generateFailedExecutionFromError(
mode: WorkflowExecuteMode,
error: NodeApiError | NodeOperationError | WorkflowOperationError,
node: INode,
): IRun {
return {
data: {
startData: {
destinationNode: node.name,
runNodeFilter: [node.name],
},
resultData: {
error,
runData: {
[node.name]: [
{
startTime: 0,
executionTime: 0,
error,
source: [],
},
],
},
lastNodeExecuted: node.name,
},
executionData: {
contextData: {},
nodeExecutionStack: [
{
node,
data: {},
source: null,
},
],
waitingExecution: {},
waitingExecutionSource: {},
},
},
finished: false,
mode,
startedAt: new Date(),
stoppedAt: new Date(),
status: 'failed',
};
}

/**
* Returns the data of the last executed node
*
Expand Down Expand Up @@ -127,6 +177,34 @@ export async function executeErrorWorkflow(
workflowErrorData.workflow.id,
);
} catch (error) {
const initialNode = workflowInstance.getStartNode();
if (initialNode) {
const errorWorkflowPermissionError = new SubworkflowOperationError(
`Another workflow: (ID ${workflowErrorData.workflow.id}) tried to invoke this workflow to handle errors.`,
"Unfortunately current permissions do not allow this. Please check that this workflow's settings allow it to be called by others",
);

// Create a fake execution and save it to DB.
const fakeExecution = generateFailedExecutionFromError(
'error',
errorWorkflowPermissionError,
initialNode,
);

const fullExecutionData: IExecutionDb = {
data: fakeExecution.data,
mode: fakeExecution.mode,
finished: false,
startedAt: new Date(),
stoppedAt: new Date(),
workflowData,
waitTill: null,
status: fakeExecution.status,
workflowId: workflowData.id,
};

await Container.get(ExecutionRepository).createNewExecution(fullExecutionData);
}
Logger.info('Error workflow execution blocked due to subworkflow settings', {
erroredWorkflowId: workflowErrorData.workflow.id,
errorWorkflowId: workflowId,
Expand Down Expand Up @@ -445,52 +523,6 @@ export async function isBelowOnboardingThreshold(user: User): Promise<boolean> {
return belowThreshold;
}

export function generateFailedExecutionFromError(
mode: WorkflowExecuteMode,
error: NodeApiError | NodeOperationError,
node: INode,
): IRun {
return {
data: {
startData: {
destinationNode: node.name,
runNodeFilter: [node.name],
},
resultData: {
error,
runData: {
[node.name]: [
{
startTime: 0,
executionTime: 0,
error,
source: [],
},
],
},
lastNodeExecuted: node.name,
},
executionData: {
contextData: {},
nodeExecutionStack: [
{
node,
data: {},
source: null,
},
],
waitingExecution: {},
waitingExecutionSource: {},
},
},
finished: false,
mode,
startedAt: new Date(),
stoppedAt: new Date(),
status: 'failed',
};
}

/** Get all nodes in a workflow where the node credential is not accessible to the user. */
export function getNodesWithInaccessibleCreds(workflow: WorkflowEntity, userCredIds: string[]) {
if (!workflow.nodes) {
Expand Down
10 changes: 3 additions & 7 deletions packages/editor-ui/src/mixins/executionsHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,15 @@ export const executionHelpers = defineComponent({
runningTime: '',
};

if (execution.status === 'waiting' || execution.waitTill) {
if (execution.status === 'waiting') {
status.name = 'waiting';
status.label = this.$locale.baseText('executionsList.waiting');
} else if (execution.status === 'canceled') {
status.label = this.$locale.baseText('executionsList.canceled');
} else if (
execution.status === 'running' ||
execution.status === 'new' ||
execution.stoppedAt === undefined
) {
} else if (execution.status === 'running' || execution.status === 'new') {
status.name = 'running';
status.label = this.$locale.baseText('executionsList.running');
} else if (execution.status === 'success' || execution.finished) {
} else if (execution.status === 'success') {
status.name = 'success';
status.label = this.$locale.baseText('executionsList.succeeded');
} else if (execution.status === 'failed' || execution.status === 'crashed') {
Expand Down
5 changes: 3 additions & 2 deletions packages/workflow/src/WorkflowErrors.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { INode } from './Interfaces';
import { ExecutionBaseError } from './NodeErrors';

/**
* Class for instantiating an operational error, e.g. a timeout error.
*/
export class WorkflowOperationError extends Error {
export class WorkflowOperationError extends ExecutionBaseError {
node: INode | undefined;

timestamp: number;
Expand All @@ -13,7 +14,7 @@ export class WorkflowOperationError extends Error {
description: string | undefined;

constructor(message: string, node?: INode) {
super(message);
super(message, { cause: undefined });
this.name = this.constructor.name;
this.node = node;
this.timestamp = Date.now();
Expand Down

0 comments on commit 67b88f7

Please sign in to comment.