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',