n8n/packages/@n8n/eslint-plugin-community-nodes/docs/rules/require-node-api-error.md

1.3 KiB

Require NodeApiError or NodeOperationError for error wrapping in catch blocks. Raw errors lose HTTP context in the n8n UI (@n8n/community-nodes/require-node-api-error)

💼 This rule is enabled in the following configs: recommended, ☑️ recommendedWithoutN8nCloudSupport.

Rule Details

When errors are caught and re-thrown in n8n nodes, they must be wrapped in NodeApiError or NodeOperationError. Raw re-throws and generic Error constructors lose HTTP context (status code, response body, etc.) that the n8n UI relies on to display meaningful error information to users.

Examples

Incorrect

try {
  await apiRequest();
} catch (error) {
  throw error;
}
try {
  await apiRequest();
} catch (error) {
  throw new Error('Request failed');
}

Correct

try {
  await apiRequest();
} catch (error) {
  throw new NodeApiError(this.getNode(), error as JsonObject);
}
try {
  await apiRequest();
} catch (error) {
  throw new NodeOperationError(this.getNode(), 'Operation failed', { itemIndex: i });
}
try {
  await apiRequest();
} catch (error) {
  if (this.continueOnFail()) {
    returnData.push({ json: { error: error.message } });
    continue;
  }
  throw new NodeApiError(this.getNode(), error as JsonObject);
}