Browse Source

fix: handle not null error

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5239/head
Pranav C 2 years ago
parent
commit
9a1970a322
  1. 15
      packages/nocodb/src/lib/meta/helpers/catchError.ts
  2. 8
      packages/nocodb/tests/unit/rest/tests/tableRow.test.ts

15
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<string, any>;
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.';

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

Loading…
Cancel
Save