From 9a1970a32235c016d69fb355f0b0c6e183a92d9f Mon Sep 17 00:00:00 2001 From: Pranav C Date: Thu, 2 Mar 2023 17:43:18 +0530 Subject: [PATCH] fix: handle not null error Signed-off-by: Pranav C --- .../nocodb/src/lib/meta/helpers/catchError.ts | 15 +++++++++++++++ .../nocodb/tests/unit/rest/tests/tableRow.test.ts | 8 ++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/packages/nocodb/src/lib/meta/helpers/catchError.ts b/packages/nocodb/src/lib/meta/helpers/catchError.ts index 1c3393ed0d..fdf1344638 100644 --- a/packages/nocodb/src/lib/meta/helpers/catchError.ts +++ b/packages/nocodb/src/lib/meta/helpers/catchError.ts @@ -5,6 +5,7 @@ enum DBError { COLUMN_NOT_EXIST = 'COLUMN_NOT_EXIST', CONSTRAINT_EXIST = 'CONSTRAINT_EXIST', CONSTRAINT_NOT_EXIST = 'CONSTRAINT_NOT_EXIST', + COLUMN_NOT_NULL = 'COLUMN_NOT_NULL', } // extract db errors using database error code @@ -20,6 +21,7 @@ function extractDBError(error): { let extra: Record; let type: DBError; + // todo: handle not null constraint error for all databases switch (error.code) { // sqlite errors case 'SQLITE_BUSY': @@ -172,6 +174,19 @@ function extractDBError(error): { break; case 'ER_BAD_NULL_ERROR': message = 'A null value is not allowed for this field.'; + { + const extractColNameMatch = error.message.match( + /Column '(\w+)' cannot be null/i + ); + if (extractColNameMatch && extractColNameMatch[1]) { + message = `The column '${extractColNameMatch[1]}' cannot be null.`; + type = DBError.COLUMN_NOT_NULL; + extra = { + column: extractColNameMatch[1], + }; + } + } + break; case 'ER_DATA_TOO_LONG': message = 'The data entered is too long for this field.'; diff --git a/packages/nocodb/tests/unit/rest/tests/tableRow.test.ts b/packages/nocodb/tests/unit/rest/tests/tableRow.test.ts index 3f2b75e7e0..9fdc944bd2 100644 --- a/packages/nocodb/tests/unit/rest/tests/tableRow.test.ts +++ b/packages/nocodb/tests/unit/rest/tests/tableRow.test.ts @@ -2125,9 +2125,13 @@ function tableTest() { .set('xc-auth', context.token) .expect(400); + // todo: only keep generic error message once updated in noco catchError middleware if ( - !response.body.msg.includes("Column 'customer_id' cannot be null") && - !response.body.msg.includes('Cannot add or update a child row') + !response.body.message?.includes("The column 'customer_id' cannot be null") && + !response.body.message?.includes("Column 'customer_id' cannot be null") && + !response.body.message?.includes('Cannot add or update a child row') && + !response.body.msg?.includes("Column 'customer_id' cannot be null") && + !response.body.msg?.includes('Cannot add or update a child row') ) { console.log( 'Delete list hm with existing ref row id with non nullable clause',