π₯ π₯ π₯ A Free & Self-hostable Airtable Alternative
TypeScript airtableairtable-alternativeautomatic-apihacktoberfestlow-codeno-codeno-code-databaseno-code-platformpostgresqlrest-apirestful-apispreadsheetsqliteswagger
parse formula parsing error to give position
Fendy Heryantobf32461c6691
diff --git a/packages/nocodb-sdk/src/lib/formulaHelpers.spec.ts b/packages/nocodb-sdk/src/lib/formulaHelpers.spec.tsindex a8ed2f692e..00f1d73896 100644--- a/packages/nocodb-sdk/src/lib/formulaHelpers.spec.ts+++ b/packages/nocodb-sdk/src/lib/formulaHelpers.spec.ts@@ -149,4 +149,50 @@ describe('Formula parsing and type validation', () => {expect(result.dataType).toBe(FormulaDataTypes.STRING);});});++ describe('errors', () => {+ it(`will provide position for syntax error`, async () => {+ try {+ await validateFormulaAndExtractTreeWithType({+ formula: '1 +',+ columns: [],+ clientOrSqlUi: 'mysql2',+ getMeta: async () => ({}),+ });+ } catch (ex) {+ expect(ex.extra.position).toEqual({+ column: 3,+ row: 0,+ });+ }+ try {+ await validateFormulaAndExtractTreeWithType({+ formula: '(1 + 1',+ columns: [],+ clientOrSqlUi: 'mysql2',+ getMeta: async () => ({}),+ });+ } catch (ex) {+ expect(ex.extra.position).toEqual({+ column: 6,+ row: 0,+ });+ }+ });+ it(`will provide position for column not found`, async () => {+ try {+ await validateFormulaAndExtractTreeWithType({+ formula: '1 + _',+ columns: [],+ clientOrSqlUi: 'mysql2',+ getMeta: async () => ({}),+ });+ } catch (ex) {+ expect(ex.extra.position).toEqual({+ column: 4,+ row: 0,+ });+ }+ });+ });});diff --git a/packages/nocodb-sdk/src/lib/formulaHelpers.ts b/packages/nocodb-sdk/src/lib/formulaHelpers.tsindex 2d651719ab..a5e8e208bc 100644--- a/packages/nocodb-sdk/src/lib/formulaHelpers.ts+++ b/packages/nocodb-sdk/src/lib/formulaHelpers.ts@@ -12,6 +12,7 @@ import UITypes from './UITypes';import dayjs from 'dayjs';import { SqlUiFactory } from './sqlUi';import { dateFormats } from './dateTimeHelper';+import { getRowColPositionFromIndex } from './stringHelpers';type SqlUI = ReturnType<(typeof SqlUiFactory)['create']>;type ClientTypeOrSqlUI =@@ -209,6 +210,7 @@ export enum FormulaErrorType {CIRCULAR_REFERENCE = 'CIRCULAR_REFERENCE',INVALID_FUNCTION_NAME = 'INVALID_FUNCTION_NAME',INVALID_COLUMN = 'INVALID_COLUMN',+ INVALID_SYNTAX = 'INVALID_SYNTAX',}export function substituteColumnIdWithAliasInFormula(@@ -1796,6 +1798,51 @@ async function extractColumnIdentifierType({return res;}+const REGEX_ERROR_AT_CHARACTER = /\sat character (\d+)/;++export function handleFormulaError({+ formula,+ error,+}: {+ formula: string;+ error;+}) {+ let position: number;+ if (error.extra?.columnName) {+ const identifierMatch = formula.match(+ new RegExp(`\\b${error.extra?.columnName}\\b`)+ );+ if (typeof identifierMatch?.index === 'number') {+ position = identifierMatch.index;+ }+ } else {+ const message: string = error.message;++ // check for error character at+ const errorAtRegex = message.match(REGEX_ERROR_AT_CHARACTER);+ if (errorAtRegex?.[0]) {+ position = Number(errorAtRegex[1]);+ }+ }+ if (position) {+ const colRowPosition = getRowColPositionFromIndex({+ stack: formula,+ position,+ });++ throw new FormulaError(+ error.type ?? FormulaErrorType.INVALID_SYNTAX,+ {+ ...(error.extra ?? {}),+ position: colRowPosition,+ },+ 'Required arguments missing'+ );+ } else {+ throw error;+ }+}+export async function validateFormulaAndExtractTreeWithType({formula,column,@@ -2159,14 +2206,19 @@ export async function validateFormulaAndExtractTreeWithType({return res;};- // register jsep curly hook- jsep.plugins.register(jsepCurlyHook);- const parsedFormula = jsep(formula);- // TODO: better jsep expression handling- const result = await validateAndExtract(- parsedFormula as unknown as ParsedFormulaNode- );- return result;+ try {+ // register jsep curly hook+ jsep.plugins.register(jsepCurlyHook);+ const parsedFormula = jsep(formula);+ // TODO: better jsep expression handling+ const result = await validateAndExtract(+ parsedFormula as unknown as ParsedFormulaNode+ );+ return result;+ } catch (ex) {+ handleFormulaError({ formula, error: ex });+ throw ex;+ }}function handleBinaryExpressionForDateAndTime(params: {diff --git a/packages/nocodb-sdk/src/lib/stringHelpers.ts b/packages/nocodb-sdk/src/lib/stringHelpers.tsindex accb0f0844..e891b43d05 100644--- a/packages/nocodb-sdk/src/lib/stringHelpers.ts+++ b/packages/nocodb-sdk/src/lib/stringHelpers.ts@@ -15,6 +15,24 @@ export enum AppendToLengthSuffix {dot = 'dot',}+/**+ * get a row & column given a numeric position+ * row & column start at 0+ */+export function getRowColPositionFromIndex({+ stack,+ position,+}: {+ stack: string;+ position: number;+}) {+ const parts = stack.substring(0, position).split('\n');+ return {+ column: parts[parts.length - 1].length,+ row: parts.length - 1,+ };+}+export async function appendToLength(param: {value: string;appendage: string;