refactor(core): Switch plain errors in workflow to ApplicationError (no-changelog) (#7877)

Ensure all errors in `workflow` are `ApplicationError` or children of it
and contain no variables in the message, to continue normalizing all the
errors we report to Sentry

Follow-up to: https://github.com/n8n-io/n8n/pull/7873
This commit is contained in:
Iván Ovejero
2023-11-30 12:46:45 +01:00
committed by GitHub
parent ce2d388f05
commit 67702c2485
9 changed files with 110 additions and 61 deletions

View File

@@ -39,6 +39,7 @@ import { WorkflowHooks } from '@/WorkflowHooks';
import * as NodeHelpers from '@/NodeHelpers';
import { deepCopy } from '@/utils';
import { getGlobalState } from '@/GlobalState';
import { ApplicationError } from '@/errors/application.error';
export interface INodeTypesObject {
[key: string]: INodeType;
@@ -55,14 +56,14 @@ export class Credentials extends ICredentials {
getData(): ICredentialDataDecryptedObject {
if (this.data === undefined) {
throw new Error('No data is set so nothing can be returned.');
throw new ApplicationError('No data is set so nothing can be returned');
}
return JSON.parse(this.data);
}
getDataToSave(): ICredentialsEncrypted {
if (this.data === undefined) {
throw new Error('No credentials were set to save.');
throw new ApplicationError('No credentials were set to save');
}
return {
@@ -135,13 +136,15 @@ export function getNodeParameter(
): NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[] | object {
const nodeType = workflow.nodeTypes.getByNameAndVersion(node.type, node.typeVersion);
if (nodeType === undefined) {
throw new Error(`Node type "${node.type}" is not known so can not return parameter value!`);
throw new ApplicationError('Node type is unknown so cannot return parameter value', {
tags: { nodeType: node.type },
});
}
const value = get(node.parameters, parameterName, fallbackValue);
if (value === undefined) {
throw new Error(`Could not get parameter "${parameterName}"!`);
throw new ApplicationError('Could not get parameter', { extra: { parameterName } });
}
let returnData;
@@ -211,12 +214,15 @@ export function getExecuteFunctions(
}
if (inputData[inputName].length < inputIndex) {
throw new Error(`Could not get input index "${inputIndex}" of input "${inputName}"!`);
throw new ApplicationError('Could not get input index', {
extra: { inputIndex, inputName },
});
}
if (inputData[inputName][inputIndex] === null) {
// return [];
throw new Error(`Value "${inputIndex}" of input "${inputName}" did not get set!`);
throw new ApplicationError('Value of input did not get set', {
extra: { inputIndex, inputName },
});
}
return inputData[inputName][inputIndex] as INodeExecutionData[];
@@ -387,21 +393,23 @@ export function getExecuteSingleFunctions(
}
if (inputData[inputName].length < inputIndex) {
throw new Error(`Could not get input index "${inputIndex}" of input "${inputName}"!`);
throw new ApplicationError('Could not get input index', {
extra: { inputIndex, inputName },
});
}
const allItems = inputData[inputName][inputIndex];
if (allItems === null) {
// return [];
throw new Error(`Value "${inputIndex}" of input "${inputName}" did not get set!`);
throw new ApplicationError('Value of input did not get set', {
extra: { inputIndex, inputName },
});
}
if (allItems[itemIndex] === null) {
// return [];
throw new Error(
`Value "${inputIndex}" of input "${inputName}" with itemIndex "${itemIndex}" did not get set!`,
);
throw new ApplicationError('Value of input with item index did not get set', {
extra: { inputIndex, inputName, itemIndex },
});
}
return allItems[itemIndex];

View File

@@ -1,3 +1,4 @@
import { ApplicationError } from '@/errors/application.error';
import { jsonParse, jsonStringify, deepCopy, isObjectEmpty, fileTypeFromMimeType } from '@/utils';
describe('isObjectEmpty', () => {
@@ -58,7 +59,11 @@ describe('isObjectEmpty', () => {
const { calls } = keySpy.mock;
const assertCalls = (count: number) => {
if (calls.length !== count) throw new Error(`Object.keys was called ${calls.length} times`);
if (calls.length !== count) {
throw new ApplicationError('`Object.keys()` was called an unexpected number of times', {
extra: { times: calls.length },
});
}
};
assertCalls(0);