Skip to content

Commit

Permalink
fix: Expand error types in node-execute-block-wrong-error-thrown (#192
Browse files Browse the repository at this point in the history
)

* fix: Expand error types in `node-execute-block-wrong-error-thrown`

* Fix missing bracket
  • Loading branch information
ivov authored Oct 6, 2024
1 parent 151615a commit 5d2410c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ In the `community` ruleset, the five `*-still-default` rules allow you to define
| [node-execute-block-double-assertion-for-items](docs/rules/node-execute-block-double-assertion-for-items.md) | In the `execute()` method there is no need to double assert the type of `items.length`. | Yes |
| [node-execute-block-error-missing-item-index](docs/rules/node-execute-block-error-missing-item-index.md) | In the operations in the `execute()` method in a node, `NodeApiError` and `NodeOperationError` must specify `itemIndex` as the third argument. | No |
| [node-execute-block-missing-continue-on-fail](docs/rules/node-execute-block-missing-continue-on-fail.md) | The `execute()` method in a node must implement `continueOnFail` in a try-catch block. | No |
| [node-execute-block-wrong-error-thrown](docs/rules/node-execute-block-wrong-error-thrown.md) | The `execute()` method in a node may only throw `NodeApiError` for failed API requests and `NodeOperationError` for internal errors, not the built-in `Error`. Refer to [`NodeErrors.ts`](https://github.com/n8n-io/n8n/blob/master/packages/workflow/src/NodeErrors.ts). | No |
| [node-execute-block-wrong-error-thrown](docs/rules/node-execute-block-wrong-error-thrown.md) | The `execute()` method in a node may only throw `ApplicationError`, NodeApiError`, `NodeOperationError`, or `TriggerCloseError`. | No |
| [node-filename-against-convention](docs/rules/node-filename-against-convention.md) | `name` in node class description must match the node filename without the `.node.ts` suffix. Example: If `description.name` is `Test`, then filename must be `Test.node.ts`. Version suffix in filename (e.g. `-V2`) is disregarded. | No |
| [node-param-array-type-assertion](docs/rules/node-param-array-type-assertion.md) | Array of node parameters must be typed, not type-asserted. | Yes |
| [node-param-collection-type-item-required](docs/rules/node-param-collection-type-item-required.md) | Items in collection-type node parameter must not have a `required` property. | Yes |
Expand Down
14 changes: 13 additions & 1 deletion docs/rules/node-execute-block-wrong-error-thrown.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# node-execute-block-wrong-error-thrown

The `execute()` method in a node may only throw `NodeApiError` for failed API requests and `NodeOperationError` for internal errors, not the built-in `Error`. Refer to [`NodeErrors.ts`](https://github.com/n8n-io/n8n/blob/master/packages/workflow/src/NodeErrors.ts).
The `execute()` method in a node may only throw `ApplicationError`, NodeApiError`, `NodeOperationError`, or `TriggerCloseError`.

📋 This rule is part of the `plugin:n8n-nodes-base/nodes` config.

Expand Down Expand Up @@ -32,6 +32,18 @@ class TestNode {
throw new NodeOperationError(this.getNode(), "An error occurred");
}
}

class TestNode {
execute() {
throw new ApplicationError("An error occurred", { level: "warning" });
}
}

class TestNode {
execute() {
throw new TriggerCloseError(this.getNode);
}
}
```

## Links
Expand Down
7 changes: 6 additions & 1 deletion lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,12 @@ export const DOCUMENTATION = {
// misc
// ----------------------------------

export const N8N_NODE_ERROR_TYPES = ["NodeOperationError", "NodeApiError"];
export const N8N_NODE_ERROR_TYPES = [
"ApplicationError",
"NodeOperationError",
"NodeApiError",
"TriggerCloseError",
];

/**
* Credentials exempted from these rules:
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/node-execute-block-wrong-error-thrown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ export default utils.createRule({
type: "problem",
docs: {
description:
"The `execute()` method in a node may only throw `NodeApiError` for failed API requests and `NodeOperationError` for internal errors, not the built-in `Error`. Refer to [`NodeErrors.ts`](https://github.com/n8n-io/n8n/blob/master/packages/workflow/src/NodeErrors.ts).",
"The `execute()` method in a node may only throw `ApplicationError`, NodeApiError`, `NodeOperationError`, or `TriggerCloseError`.",
recommended: "strict",
},
schema: [],
messages: {
useProperError:
"Use `NodeApiError` or `NodeOperationError` [non-autofixable]",
"Use `ApplicationError`, NodeApiError`, `NodeOperationError`, or `TriggerCloseError` [non-autofixable]",
},
},
defaultOptions: [],
Expand Down
16 changes: 16 additions & 0 deletions tests/node-execute-block-wrong-error-thrown.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ ruleTester().run(getRuleName(module), rule, {
}
}`,
},
{
code: outdent`
class TestNode {
execute() {
throw new ApplicationError('An error occurred', { level: "warning" });
}
}`,
},
{
code: outdent`
class TestNode {
execute() {
throw new TriggerCloseError(this.getNode());
}
}`,
},
],
invalid: [
{
Expand Down

0 comments on commit 5d2410c

Please sign in to comment.