fix(editor): Add safety to prevent undefined errors (#11104)

This commit is contained in:
Elias Meire 2024-10-04 15:37:37 +02:00 committed by GitHub
parent 1d14557461
commit 565b117a52
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 18 additions and 4 deletions

View file

@ -694,7 +694,7 @@ export function useWorkflowHelpers(options: { router: ReturnType<typeof useRoute
}
const workflowId = workflowsStore.workflowId;
const path = getWebhookExpressionValue(webhookData, 'path', true, node.name);
const path = getWebhookExpressionValue(webhookData, 'path', true, node.name) ?? '';
const isFullPath =
(getWebhookExpressionValue(
webhookData,

View file

@ -1,5 +1,9 @@
import { ExpressionError } from 'n8n-workflow';
import { stringifyExpressionResult, unwrapExpression } from '../expressions';
import {
removeExpressionPrefix,
stringifyExpressionResult,
unwrapExpression,
} from '../expressions';
describe('Utils: Expressions', () => {
describe('stringifyExpressionResult()', () => {
@ -43,4 +47,14 @@ describe('Utils: Expressions', () => {
expect(unwrapExpression('{{ $json.foo }}')).toBe('$json.foo');
});
});
describe('removeExpressionPrefix', () => {
it.each([
['=expression', 'expression'],
['notAnExpression', 'notAnExpression'],
[undefined, ''],
])('turns "%s" into "%s"', (input, output) => {
expect(removeExpressionPrefix(input)).toBe(output);
});
});
});

View file

@ -16,8 +16,8 @@ export const unwrapExpression = (expr: string) => {
return expr.replace(/\{\{(.*)\}\}/, '$1').trim();
};
export const removeExpressionPrefix = (expr: string) => {
return expr.startsWith('=') ? expr.slice(1) : expr;
export const removeExpressionPrefix = (expr: string | null | undefined) => {
return expr?.startsWith('=') ? expr.slice(1) : (expr ?? '');
};
export const isTestableExpression = (expr: string) => {