From 8db6bbaa2308e23c73d9d75e5dfbc63a0af68a3e Mon Sep 17 00:00:00 2001 From: Pranav C Date: Fri, 19 Jan 2024 09:20:15 +0000 Subject: [PATCH] fix: formula - better error message with parameter position --- packages/nc-gui/lang/en.json | 1 + packages/nocodb-sdk/src/lib/formulaHelpers.ts | 30 ++++++++++++------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/packages/nc-gui/lang/en.json b/packages/nc-gui/lang/en.json index b001e929a2..b9350165d5 100644 --- a/packages/nc-gui/lang/en.json +++ b/packages/nc-gui/lang/en.json @@ -972,6 +972,7 @@ "hintStart": "Hint: Use {placeholder1} to reference fields, e.g: {placeholder2}. For more, please check out", "hintEnd": "Formulas.", "noSuggestedFormulaFound": "No suggested formula found", + "typeIsExpected": "{calleeName} requires a {type} at position {position}", "numericTypeIsExpected": "Numeric type is expected", "stringTypeIsExpected": "String type is expected", "operationNotAvailable": "{operation} operation not available", diff --git a/packages/nocodb-sdk/src/lib/formulaHelpers.ts b/packages/nocodb-sdk/src/lib/formulaHelpers.ts index 7f9f6b0bf7..d4158a8508 100644 --- a/packages/nocodb-sdk/src/lib/formulaHelpers.ts +++ b/packages/nocodb-sdk/src/lib/formulaHelpers.ts @@ -722,10 +722,12 @@ export const formulas: Record = { throw new FormulaError( FormulaErrorType.INVALID_ARG, { - key: 'msg.formula.numericTypeIsExpected', + key: 'msg.formula.typeIsExpected', + type: 'Numeric', calleeName: parsedTree.callee?.name?.toUpperCase(), + position: 2, }, - 'Numeric type is expected' + 'The REPEAT function requires a numeric as the parameter at position 2' ); } }, @@ -1771,26 +1773,32 @@ export async function validateFormulaAndExtractTreeWithType({ `Field ${name} with ${argPt.dataType} type is found but ${expectedArgType} type is expected` ); } else { - let key = '', - message = 'Invalid argument type'; + let key = ''; + const position = i + 1; + let type = ''; + if (expectedArgType === FormulaDataTypes.NUMERIC) { - key = 'msg.formula.numericTypeIsExpected'; - message = 'Numeric type is expected'; + key = 'msg.formula.typeIsExpected'; + type = 'numeric'; } else if (expectedArgType === FormulaDataTypes.BOOLEAN) { - key = 'msg.formula.booleanTypeIsExpected'; - message = 'Boolean type is expected'; + key = 'msg.formula.typeIsExpected'; + type = 'boolean'; } else if (expectedArgType === FormulaDataTypes.DATE) { - key = 'msg.formula.dateTypeIsExpected'; - message = 'Date type is expected'; + key = 'msg.formula.typeIsExpected'; + type = 'date'; } throw new FormulaError( FormulaErrorType.INVALID_ARG, { + type, key, + position, calleeName, }, - message + `${calleeName?.toUpperCase()} requires a ${ + type || expectedArgType + } at position ${position}` ); } }