Browse Source

fix: formula errors

pull/7268/head
Pranav C 11 months ago
parent
commit
c7b265e429
  1. 75
      packages/nocodb-sdk/src/lib/formulaHelpers.ts

75
packages/nocodb-sdk/src/lib/formulaHelpers.ts

@ -976,17 +976,19 @@ enum FormulaErrorType {
} }
class FormulaError extends Error { class FormulaError extends Error {
public name: string;
public type: FormulaErrorType; public type: FormulaErrorType;
public extra: Record<string, any>;
constructor( constructor(
type: FormulaErrorType, type: FormulaErrorType,
name: string, extra: {
[key: string]: any;
},
message: string = 'Formula Error' message: string = 'Formula Error'
) { ) {
super(message); super(message);
this.name = name;
this.type = type; this.type = type;
this.extra = extra;
} }
} }
@ -1029,48 +1031,39 @@ export function validateFormulaAndExtractTreeWithType(
) { ) {
throw new FormulaError( throw new FormulaError(
FormulaErrorType.INVALID_ARG, FormulaErrorType.INVALID_ARG,
calleeName, {
key: 'msg.formula.requiredArgumentsFormula',
requiredArguments: validation.args.rqd,
calleeName,
},
'Required arguments missing' 'Required arguments missing'
); );
// errors.add(
// t('msg.formula.requiredArgumentsFormula', {
// requiredArguments: validation.args.rqd,
// calleeName,
// })
// );
} else if ( } else if (
validation.args.min !== undefined && validation.args.min !== undefined &&
validation.args.min > parsedTree.arguments.length validation.args.min > parsedTree.arguments.length
) { ) {
throw new FormulaError( throw new FormulaError(
FormulaErrorType.MIN_ARG, FormulaErrorType.MIN_ARG,
calleeName, {
key: 'msg.formula.minRequiredArgumentsFormula',
minRequiredArguments: validation.args.min,
calleeName,
},
'Minimum arguments required' 'Minimum arguments required'
); );
// errors.add(
// t('msg.formula.minRequiredArgumentsFormula', {
// minRequiredArguments: validation.args.min,
// calleeName,
// })
// );
} else if ( } else if (
validation.args.max !== undefined && validation.args.max !== undefined &&
validation.args.max < parsedTree.arguments.length validation.args.max < parsedTree.arguments.length
) { ) {
throw new FormulaError( throw new FormulaError(
FormulaErrorType.MAX_ARG, FormulaErrorType.INVALID_ARG,
calleeName, {
'Maximum arguments required' key: 'msg.formula.maxRequiredArgumentsFormula',
maxRequiredArguments: validation.args.max,
calleeName,
},
'Maximum arguments missing'
); );
// errors.add(
// t('msg.formula.maxRequiredArgumentsFormula', {
// maxRequiredArguments: validation.args.max,
// calleeName,
// })
// );
} }
} }
// get args type and validate // get args type and validate
@ -1098,12 +1091,14 @@ export function validateFormulaAndExtractTreeWithType(
// check for circular reference // check for circular reference
checkForCircularFormulaRef(col, parsedTree, columns); checkForCircularFormulaRef(col, parsedTree, columns);
const formulaRes = col.colOptions?.parsed_tree || validateFormulaAndExtractTreeWithType( const formulaRes =
// formula may include double curly brackets in previous version col.colOptions?.parsed_tree ||
// convert to single curly bracket here for compatibility validateFormulaAndExtractTreeWithType(
col.colOptions.formula.replaceAll('{{', '{').replaceAll('}}', '}'), // formula may include double curly brackets in previous version
columns // convert to single curly bracket here for compatibility
); col.colOptions.formula.replaceAll('{{', '{').replaceAll('}}', '}'),
columns
);
res.dataType = formulaRes as any; res.dataType = formulaRes as any;
} else { } else {
@ -1268,9 +1263,13 @@ function checkForCircularFormulaRef(formulaCol, parsedTree, columns) {
} }
// vertices not same as visited = cycle found // vertices not same as visited = cycle found
if (vertices !== visited) { if (vertices !== visited) {
// errors.add(t('msg.formula.cantSaveCircularReference')) throw new FormulaError(
throw new FormulaError(FormulaErrorType.CIRCULAR_REFERENCE, ''); FormulaErrorType.CIRCULAR_REFERENCE,
{
key: 'msg.formula.cantSaveCircularReference',
},
'Circular reference detected'
);
} }
} }
} }

Loading…
Cancel
Save