Skip to content

πŸ”₯ πŸ”₯ πŸ”₯ A Free & Self-hostable Airtable Alternative

refactor to db error extractor

Fendy Heryanto6ae4ed96db28
diff --git a/packages/nocodb/src/helpers/catchError.ts b/packages/nocodb/src/helpers/catchError.ts
index e5a3530a7a..a07a2213f2 100644
--- a/packages/nocodb/src/helpers/catchError.ts
+++ b/packages/nocodb/src/helpers/catchError.ts
@@ -1,5 +1,4 @@
-import { Logger } from '@nestjs/common';
-import { NcErrorType } from 'nocodb-sdk';
+import { DBErrorExtractor } from './db-error/extractor';
export {
NcBaseError,
NcBaseErrorv2,
@@ -18,19 +17,6 @@ export {
} from 'nocodb-sdk';
export { AjvError, NcError } from '~/helpers/ncError';
-const dbErrorLogger = new Logger('MissingDBError');
-
-export enum DBError {
- TABLE_EXIST = 'TABLE_EXIST',
- TABLE_NOT_EXIST = 'TABLE_NOT_EXIST',
- COLUMN_EXIST = 'COLUMN_EXIST',
- COLUMN_NOT_EXIST = 'COLUMN_NOT_EXIST',
- CONSTRAINT_EXIST = 'CONSTRAINT_EXIST',
- CONSTRAINT_NOT_EXIST = 'CONSTRAINT_NOT_EXIST',
- COLUMN_NOT_NULL = 'COLUMN_NOT_NULL',
- DATA_TYPE_MISMATCH = 'DATA_TYPE_MISMATCH',
-}
-
// extract db errors using database error code
export function extractDBError(error): {
message: string;
@@ -39,410 +25,5 @@ export function extractDBError(error): {
code?: string;
httpStatus: number;
} | void {
- if (!error.code) return;
-
- let message: string;
- let _extra: Record<string, any>;
- let _type: DBError;
- let httpStatus = 422;
-
- // todo: handle not null constraint error for all databases
- switch (error.code) {
- // sqlite errors
- case 'SQLITE_BUSY':
- message = 'The database is locked by another process or transaction.';
- httpStatus = 500;
- break;
- case 'SQLITE_CONSTRAINT':
- {
- const constraint = /FOREIGN KEY|UNIQUE/.test(error.message)
- ? error.message.match(/FOREIGN KEY|UNIQUE/gi)?.join(' ')
- : 'constraint';
- message = `A ${constraint} constraint was violated: ${error.message}`;
- _extra = {
- constraint,
- };
- }
- break;
- case 'SQLITE_CORRUPT':
- message = 'The database file is corrupt.';
- httpStatus = 500;
- break;
-
- case 'SQLITE_MISMATCH':
- if (error.message) {
- message = 'Data type mismatch in SQLite operation.';
- _type = DBError.DATA_TYPE_MISMATCH;
- }
- break;
- case 'SQLITE_ERROR':
- message = 'A SQL error occurred.';
-
- if (error.message) {
- const noSuchTableMatch = error.message.match(/no such table: (\w+)/);
- const tableAlreadyExistsMatch = error.message.match(
- /SQLITE_ERROR: table `?(\w+)`? already exists/,
- );
-
- const duplicateColumnExistsMatch = error.message.match(
- /SQLITE_ERROR: duplicate column name: (\w+)/,
- );
- const unrecognizedTokenMatch = error.message.match(
- /SQLITE_ERROR: unrecognized token: "(\w+)"/,
- );
- const columnDoesNotExistMatch = error.message.match(
- /SQLITE_ERROR: no such column: (\w+)/,
- );
- const constraintFailedMatch = error.message.match(
- /SQLITE_ERROR: constraint failed: (\w+)/,
- );
-
- if (noSuchTableMatch && noSuchTableMatch[1]) {
- message = `The table '${noSuchTableMatch[1]}' does not exist.`;
- _type = DBError.TABLE_NOT_EXIST;
- _extra = {
- table: noSuchTableMatch[1],
- };
- } else if (tableAlreadyExistsMatch && tableAlreadyExistsMatch[1]) {
- message = `The table '${tableAlreadyExistsMatch[1]}' already exists.`;
- _type = DBError.TABLE_EXIST;
- _extra = {
- table: tableAlreadyExistsMatch[1],
- };
- } else if (unrecognizedTokenMatch && unrecognizedTokenMatch[1]) {
- message = `Unrecognized token: ${unrecognizedTokenMatch[1]}`;
- _extra = {
- token: unrecognizedTokenMatch[1],
- };
- } else if (columnDoesNotExistMatch && columnDoesNotExistMatch[1]) {
- message = `The column ${columnDoesNotExistMatch[1]} does not exist.`;
- _type = DBError.COLUMN_NOT_EXIST;
- _extra = {
- column: columnDoesNotExistMatch[1],
- };
- } else if (constraintFailedMatch && constraintFailedMatch[1]) {
- message = `A constraint failed: ${constraintFailedMatch[1]}`;
- } else if (
- duplicateColumnExistsMatch &&
- duplicateColumnExistsMatch[1]
- ) {
- message = `The column '${duplicateColumnExistsMatch[1]}' already exists.`;
- _type = DBError.COLUMN_EXIST;
- _extra = {
- column: duplicateColumnExistsMatch[1],
- };
- } else {
- const match = error.message.match(/SQLITE_ERROR:\s*(\w+)/);
- if (match && match[1]) {
- message = match[1];
- }
- }
- }
- break;
- case 'SQLITE_RANGE':
- message = 'A column index is out of range.';
- break;
- case 'SQLITE_SCHEMA':
- message = 'The database schema has changed.';
- break;
-
- // mysql errors
- case 'ER_TRUNCATED_WRONG_VALUE':
- case 'ER_WRONG_VALUE':
- if (error.message) {
- const typeMismatchMatch = error.message.match(
- /Incorrect (\w+) value: (.+) for column '(\w+)'/i,
- );
- if (typeMismatchMatch) {
- const dataType = typeMismatchMatch[1];
- const invalidValue = typeMismatchMatch[2];
- const columnName = typeMismatchMatch[3];
-
- message = `Invalid ${dataType} value '${invalidValue}' for column '${columnName}'`;
- _type = DBError.DATA_TYPE_MISMATCH;
- _extra = { dataType, column: columnName, value: invalidValue };
- } else {
- message = 'Invalid data format for a column.';
- _type = DBError.DATA_TYPE_MISMATCH;
- }
- }
- break;
- case 'ER_TABLE_EXISTS_ERROR':
- message = 'The table already exists.';
-
- if (error.message) {
- const extractTableNameMatch = error.message.match(
- / Table '?(\w+)'? already exists/i,
- );
- if (extractTableNameMatch && extractTableNameMatch[1]) {
- message = `The table '${extractTableNameMatch[1]}' already exists.`;
- _type = DBError.TABLE_EXIST;
- _extra = {
- table: extractTableNameMatch[1],
- };
- }
- }
- break;
- case 'ER_DUP_FIELDNAME':
- message = 'The column already exists.';
-
- if (error.message) {
- const extractColumnNameMatch = error.message.match(
- / Duplicate column name '(\w+)'/i,
- );
- if (extractColumnNameMatch && extractColumnNameMatch[1]) {
- message = `The column '${extractColumnNameMatch[1]}' already exists.`;
- _type = DBError.COLUMN_EXIST;
- _extra = {
- column: extractColumnNameMatch[1],
- };
- }
- }
-
- break;
- case 'ER_NO_SUCH_TABLE':
- message = 'The table does not exist.';
-
- if (error.message) {
- const missingTableMatch = error.message.match(
- / Table '(?:\w+\.)?(\w+)' doesn't exist/i,
- );
- if (missingTableMatch && missingTableMatch[1]) {
- message = `The table '${missingTableMatch[1]}' does not exist`;
- _type = DBError.TABLE_NOT_EXIST;
- _extra = {
- table: missingTableMatch[1],
- };
- }
- }
-
- break;
- case 'ER_DUP_ENTRY':
- message = 'This record already exists.';
- break;
- case 'ER_PARSE_ERROR':
- message = 'There was a syntax error in your SQL query.';
- break;
- case 'ER_NO_DEFAULT_FOR_FIELD':
- message = 'A value is required for this field.';
- 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.';
- break;
- case 'ER_BAD_FIELD_ERROR':
- {
- message = 'The field you are trying to access does not exist.';
- const extractColNameMatch = error.message.match(
- / Unknown column '(\w+)' in 'field list'/i,
- );
- if (extractColNameMatch && extractColNameMatch[1]) {
- message = `The column '${extractColNameMatch[1]}' does not exist.`;
- _type = DBError.COLUMN_NOT_EXIST;
- _extra = {
- column: extractColNameMatch[1],
- };
- }
- }
- break;
- case 'ER_ACCESS_DENIED_ERROR':
- message = 'You do not have permission to perform this action.';
- httpStatus = 403;
- break;
- case 'ER_LOCK_WAIT_TIMEOUT':
- message = 'A timeout occurred while waiting for a table lock.';
- httpStatus = 500;
- break;
- case 'ER_NO_REFERENCED_ROW':
- message = 'The referenced record does not exist.';
- break;
- case 'ER_ROW_IS_REFERENCED':
- message = 'This record is being referenced by other records.';
- break;
-
- // postgres errors
- case '23505':
- message = 'This record already exists.';
- break;
- case '42601':
- message = 'There was a syntax error in your SQL query.';
- break;
- case '23502':
- message = 'A value is required for this field.';
- break;
- case '23503':
- message =
- 'Cannot delete this record because other records depend on it. Please remove the dependent records first.';
- break;
- case '23514':
- message = 'A null value is not allowed for this field.';
- break;
- case '22001':
- message = 'The data entered is too long for this field.';
- break;
- case '22007':
- message = 'The date / time value is invalid';
- break;
- case '28000':
- message = 'You do not have permission to perform this action.';
- httpStatus = 401;
- break;
- case '40P01':
- message = 'A timeout occurred while waiting for a table lock.';
- httpStatus = 500;
- break;
- case '23506':
- message = 'This record is being referenced by other records.';
- break;
- case '3D000':
- message = 'The database does not exist.';
- break;
- case '42P07':
- message = 'The table already exists.';
- if (error.message) {
- const extractTableNameMatch = error.message.match(
- / relation "?(\w+)"? already exists/i,
- );
- if (extractTableNameMatch && extractTableNameMatch[1]) {
- message = `The table '${extractTableNameMatch[1]}' already exists.`;
- _type = DBError.TABLE_EXIST;
- _extra = {
- table: extractTableNameMatch[1],
- };
- }
- }
- break;
- case '22P02': // PostgreSQL invalid_text_representation
- case '22003': // PostgreSQL numeric_value_out_of_range
- if (error.message) {
- const regexCandidates = [
- /invalid input syntax for (\w+): "(.+)"(?: in column "(\w+)")?/i,
- /invalid input syntax for type (\w+): "([^"]+)"?/i,
- ];
-
- let matched = false;
- for (const regExp of regexCandidates) {
- const pgTypeMismatchMatch = error.message.match(regExp);
- if (pgTypeMismatchMatch) {
- const dataType = pgTypeMismatchMatch[1];
- const invalidValue = pgTypeMismatchMatch[2];
- const columnName = pgTypeMismatchMatch[3];
-
- if (columnName) {
- message = `Invalid ${dataType} value '${invalidValue}' for column '${columnName}'`;
- } else {
- message = `Invalid value '${invalidValue}' for type '${dataType}'`;
- }
- _type = DBError.DATA_TYPE_MISMATCH;
- _extra = { dataType, column: columnName, value: invalidValue };
- matched = true;
- break;
- }
- }
- if (!matched) {
- const detailMatch = error.detail
- ? error.detail.match(/Column (\w+)/)
- : null;
-
- const columnName =
- detailMatch?.[1] ??
- error.message.match(/ set\s+"([^"]+)"/)?.[1] ??
- 'unknown';
- message = `Invalid data type or value for column '${columnName}'.`;
- _type = DBError.DATA_TYPE_MISMATCH;
- _extra = { column: columnName };
- }
- }
- break;
- case '42701':
- message = 'The column already exists.';
- if (error.message) {
- const extractTableNameMatch = error.message.match(
- / column "(\w+)" of relation "(\w+)" already exists/i,
- );
- if (extractTableNameMatch && extractTableNameMatch[1]) {
- message = `The column '${extractTableNameMatch[1]}' already exists.`;
- _type = DBError.COLUMN_EXIST;
- _extra = {
- column: extractTableNameMatch[1],
- };
- }
- }
- break;
- case '42P01':
- message = 'The table does not exist.';
- if (error.message) {
- const extractTableNameMatch = error.message.match(
- / relation "(\w+)" does not exist/i,
- );
- if (extractTableNameMatch && extractTableNameMatch[1]) {
- message = `The table '${extractTableNameMatch[1]}' does not exist.`;
- _type = DBError.TABLE_NOT_EXIST;
- _extra = {
- table: extractTableNameMatch[1],
- };
- }
-
- const extractColumnNameMatch = error.message.match(
- / column "(\w+)" does not exist/i,
- );
-
- if (extractColumnNameMatch && extractColumnNameMatch[1]) {
- message = `The column '${extractColumnNameMatch[1]}' does not exist.`;
- _type = DBError.COLUMN_NOT_EXIST;
- _extra = {
- table: extractColumnNameMatch[1],
- };
- }
- }
- break;
- case '42703':
- message = 'The column does not exist.';
- if (error.message) {
- const extractTableNameMatch = error.message.match(
- / column "(\w+)" does not exist/i,
- );
- if (extractTableNameMatch && extractTableNameMatch[1]) {
- message = `The column '${extractTableNameMatch[1]}' does not exist.`;
- _type = DBError.COLUMN_NOT_EXIST;
- _extra = {
- column: extractTableNameMatch[1],
- };
- }
- }
- break;
- default:
- // log error for unknown error code
- dbErrorLogger.error(error);
-
- // if error message contains -- then extract message after --
- if (error.message && error.message.includes('--')) {
- message = error.message.split('--')[1];
- }
- break;
- }
-
- if (message) {
- return {
- error: NcErrorType.DATABASE_ERROR,
- message,
- code: error.code,
- httpStatus,
- };
- }
+ return DBErrorExtractor.get().extractDbError(error);
}
diff --git a/packages/nocodb/src/helpers/db-error/default.extractor.ts b/packages/nocodb/src/helpers/db-error/default.extractor.ts
new file mode 100644
index 0000000000..03de0a9d72
--- /dev/null
+++ b/packages/nocodb/src/helpers/db-error/default.extractor.ts
@@ -0,0 +1,36 @@
+import { NcErrorType } from 'nocodb-sdk';
+import type { DBError } from './utils';
+import type { Logger } from '@nestjs/common';
+import type { DBErrorExtractResult, IClientDbErrorExtractor } from './utils';
+
+export class DefaultDBErrorExtractor implements IClientDbErrorExtractor {
+ constructor(
+ private readonly option?: {
+ dbErrorLogger?: Logger;
+ },
+ ) {}
+
+ extract(error: any): DBErrorExtractResult {
+ if (!error.code) return;
+
+ let message: string;
+ let _extra: Record<string, any>;
+ let _type: DBError;
+ const httpStatus = 422;
+
+ // log error for unknown error code
+ this.option?.dbErrorLogger?.error(error);
+
+ // if error message contains -- then extract message after --
+ if (error.message && error.message.includes('--')) {
+ message = error.message.split('--')[1];
+ }
+
+ return {
+ error: NcErrorType.DATABASE_ERROR,
+ message,
+ code: error.code,
+ httpStatus,
+ };
+ }
+}
diff --git a/packages/nocodb/src/helpers/db-error/extractor.ts b/packages/nocodb/src/helpers/db-error/extractor.ts
new file mode 100644
index 0000000000..9ccdeb251c
--- /dev/null
+++ b/packages/nocodb/src/helpers/db-error/extractor.ts
@@ -0,0 +1,59 @@
+import { ClientType } from 'nocodb-sdk';
+import { Logger } from '@nestjs/common';
+import { PgDBErrorExtractor } from './pg.extractor';
+import { SqliteDBErrorExtractor } from './sqlite.extractor';
+import { MysqlDBErrorExtractor } from './mysql.extractor';
+import { DefaultDBErrorExtractor } from './default.extractor';
+import type { DBErrorExtractResult, IClientDbErrorExtractor } from './utils';
+
+export class DBErrorExtractor {
+ constructor() {}
+ static _: DBErrorExtractor = new DBErrorExtractor();
+ static get() {
+ return DBErrorExtractor._;
+ }
+ logger = new Logger('MissingDBError');
+ extractors = new Map<ClientType, IClientDbErrorExtractor>([
+ [
+ ClientType.PG,
+ new PgDBErrorExtractor({
+ dbErrorLogger: this.logger,
+ }),
+ ],
+ [
+ ClientType.SQLITE,
+ new SqliteDBErrorExtractor({
+ dbErrorLogger: this.logger,
+ }),
+ ],
+ [
+ ClientType.MYSQL,
+ new MysqlDBErrorExtractor({
+ dbErrorLogger: this.logger,
+ }),
+ ],
+ ]);
+ defaultExtractor = new DefaultDBErrorExtractor({
+ dbErrorLogger: this.logger,
+ });
+
+ extractDbError(error: any, option?: { clientType: ClientType }) {
+ const clientType = option?.clientType;
+ let extractResult: DBErrorExtractResult;
+ if (clientType) {
+ extractResult = this.extractors.get(clientType)?.extract(error);
+ } else {
+ [ClientType.PG, ClientType.MYSQL, ClientType.SQLITE].forEach(
+ (clientType) => {
+ if (!extractResult) {
+ extractResult = this.extractors.get(clientType)?.extract(error);
+ }
+ },
+ );
+ }
+ if (!extractResult) {
+ extractResult = this.defaultExtractor.extract(error);
+ }
+ return extractResult;
+ }
+}
diff --git a/packages/nocodb/src/helpers/db-error/mysql.extractor.ts b/packages/nocodb/src/helpers/db-error/mysql.extractor.ts
new file mode 100644
index 0000000000..d8aaddec90
--- /dev/null
+++ b/packages/nocodb/src/helpers/db-error/mysql.extractor.ts
@@ -0,0 +1,163 @@
+import { NcErrorType } from 'nocodb-sdk';
+import { DBError } from './utils';
+import type { Logger } from '@nestjs/common';
+import type { DBErrorExtractResult, IClientDbErrorExtractor } from './utils';
+
+export class MysqlDBErrorExtractor implements IClientDbErrorExtractor {
+ constructor(
+ private readonly option?: {
+ dbErrorLogger?: Logger;
+ },
+ ) {}
+
+ extract(error: any): DBErrorExtractResult {
+ if (!error.code) return;
+
+ let message: string;
+ let _extra: Record<string, any>;
+ let _type: DBError;
+ let httpStatus = 422;
+
+ // todo: handle not null constraint error for all databases
+ switch (error.code) {
+ // mysql errors
+ case 'ER_TRUNCATED_WRONG_VALUE':
+ case 'ER_WRONG_VALUE':
+ if (error.message) {
+ const typeMismatchMatch = error.message.match(
+ /Incorrect (\w+) value: (.+) for column '(\w+)'/i,
+ );
+ if (typeMismatchMatch) {
+ const dataType = typeMismatchMatch[1];
+ const invalidValue = typeMismatchMatch[2];
+ const columnName = typeMismatchMatch[3];
+
+ message = `Invalid ${dataType} value '${invalidValue}' for column '${columnName}'`;
+ _type = DBError.DATA_TYPE_MISMATCH;
+ _extra = { dataType, column: columnName, value: invalidValue };
+ } else {
+ message = 'Invalid data format for a column.';
+ _type = DBError.DATA_TYPE_MISMATCH;
+ }
+ }
+ break;
+ case 'ER_TABLE_EXISTS_ERROR':
+ message = 'The table already exists.';
+
+ if (error.message) {
+ const extractTableNameMatch = error.message.match(
+ / Table '?(\w+)'? already exists/i,
+ );
+ if (extractTableNameMatch && extractTableNameMatch[1]) {
+ message = `The table '${extractTableNameMatch[1]}' already exists.`;
+ _type = DBError.TABLE_EXIST;
+ _extra = {
+ table: extractTableNameMatch[1],
+ };
+ }
+ }
+ break;
+ case 'ER_DUP_FIELDNAME':
+ message = 'The column already exists.';
+
+ if (error.message) {
+ const extractColumnNameMatch = error.message.match(
+ / Duplicate column name '(\w+)'/i,
+ );
+ if (extractColumnNameMatch && extractColumnNameMatch[1]) {
+ message = `The column '${extractColumnNameMatch[1]}' already exists.`;
+ _type = DBError.COLUMN_EXIST;
+ _extra = {
+ column: extractColumnNameMatch[1],
+ };
+ }
+ }
+
+ break;
+ case 'ER_NO_SUCH_TABLE':
+ message = 'The table does not exist.';
+
+ if (error.message) {
+ const missingTableMatch = error.message.match(
+ / Table '(?:\w+\.)?(\w+)' doesn't exist/i,
+ );
+ if (missingTableMatch && missingTableMatch[1]) {
+ message = `The table '${missingTableMatch[1]}' does not exist`;
+ _type = DBError.TABLE_NOT_EXIST;
+ _extra = {
+ table: missingTableMatch[1],
+ };
+ }
+ }
+
+ break;
+ case 'ER_DUP_ENTRY':
+ message = 'This record already exists.';
+ break;
+ case 'ER_PARSE_ERROR':
+ message = 'There was a syntax error in your SQL query.';
+ break;
+ case 'ER_NO_DEFAULT_FOR_FIELD':
+ message = 'A value is required for this field.';
+ 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.';
+ break;
+ case 'ER_BAD_FIELD_ERROR':
+ {
+ message = 'The field you are trying to access does not exist.';
+ const extractColNameMatch = error.message.match(
+ / Unknown column '(\w+)' in 'field list'/i,
+ );
+ if (extractColNameMatch && extractColNameMatch[1]) {
+ message = `The column '${extractColNameMatch[1]}' does not exist.`;
+ _type = DBError.COLUMN_NOT_EXIST;
+ _extra = {
+ column: extractColNameMatch[1],
+ };
+ }
+ }
+ break;
+ case 'ER_ACCESS_DENIED_ERROR':
+ message = 'You do not have permission to perform this action.';
+ httpStatus = 403;
+ break;
+ case 'ER_LOCK_WAIT_TIMEOUT':
+ message = 'A timeout occurred while waiting for a table lock.';
+ httpStatus = 500;
+ break;
+ case 'ER_NO_REFERENCED_ROW':
+ message = 'The referenced record does not exist.';
+ break;
+ case 'ER_ROW_IS_REFERENCED':
+ message = 'This record is being referenced by other records.';
+ break;
+
+ default:
+ return;
+ }
+
+ return {
+ error: NcErrorType.DATABASE_ERROR,
+ message,
+ code: error.code,
+ httpStatus,
+ };
+ }
+}
diff --git a/packages/nocodb/src/helpers/db-error/pg.extractor.ts b/packages/nocodb/src/helpers/db-error/pg.extractor.ts
new file mode 100644
index 0000000000..480996271d
--- /dev/null
+++ b/packages/nocodb/src/helpers/db-error/pg.extractor.ts
@@ -0,0 +1,189 @@
+import { NcErrorType } from 'nocodb-sdk';
+import { DBError } from './utils';
+import type { Logger } from '@nestjs/common';
+import type { DBErrorExtractResult, IClientDbErrorExtractor } from './utils';
+
+export class PgDBErrorExtractor implements IClientDbErrorExtractor {
+ constructor(
+ private readonly option?: {
+ dbErrorLogger?: Logger;
+ },
+ ) {}
+
+ extract(error: any): DBErrorExtractResult {
+ if (!error.code) return;
+
+ let message: string;
+ let _extra: Record<string, any>;
+ let _type: DBError;
+ let httpStatus = 422;
+
+ // todo: handle not null constraint error for all databases
+ switch (error.code) {
+ // postgres errors
+ case '23505':
+ message = 'This record already exists.';
+ break;
+ case '42601':
+ message = 'There was a syntax error in your SQL query.';
+ break;
+ case '23502':
+ message = 'A value is required for this field.';
+ break;
+ case '23503':
+ message =
+ 'Cannot delete this record because other records depend on it. Please remove the dependent records first.';
+ break;
+ case '23514':
+ message = 'A null value is not allowed for this field.';
+ break;
+ case '22001':
+ message = 'The data entered is too long for this field.';
+ break;
+ case '22007':
+ message = 'The date / time value is invalid.';
+ break;
+ case '22011':
+ message = 'Negative substring length not allowed.';
+ break;
+ case '28000':
+ message = 'You do not have permission to perform this action.';
+ httpStatus = 401;
+ break;
+ case '40P01':
+ message = 'A timeout occurred while waiting for a table lock.';
+ httpStatus = 500;
+ break;
+ case '23506':
+ message = 'This record is being referenced by other records.';
+ break;
+ case '3D000':
+ message = 'The database does not exist.';
+ break;
+ case '42P07':
+ message = 'The table already exists.';
+ if (error.message) {
+ const extractTableNameMatch = error.message.match(
+ / relation "?(\w+)"? already exists/i,
+ );
+ if (extractTableNameMatch && extractTableNameMatch[1]) {
+ message = `The table '${extractTableNameMatch[1]}' already exists.`;
+ _type = DBError.TABLE_EXIST;
+ _extra = {
+ table: extractTableNameMatch[1],
+ };
+ }
+ }
+ break;
+ case '22P02': // PostgreSQL invalid_text_representation
+ case '22003': // PostgreSQL numeric_value_out_of_range
+ if (error.message) {
+ const regexCandidates = [
+ /invalid input syntax for (\w+): "(.+)"(?: in column "(\w+)")?/i,
+ /invalid input syntax for type (\w+): "([^"]+)"?/i,
+ ];
+
+ let matched = false;
+ for (const regExp of regexCandidates) {
+ const pgTypeMismatchMatch = error.message.match(regExp);
+ if (pgTypeMismatchMatch) {
+ const dataType = pgTypeMismatchMatch[1];
+ const invalidValue = pgTypeMismatchMatch[2];
+ const columnName = pgTypeMismatchMatch[3];
+
+ if (columnName) {
+ message = `Invalid ${dataType} value '${invalidValue}' for column '${columnName}'`;
+ } else {
+ message = `Invalid value '${invalidValue}' for type '${dataType}'`;
+ }
+ _type = DBError.DATA_TYPE_MISMATCH;
+ _extra = { dataType, column: columnName, value: invalidValue };
+ matched = true;
+ break;
+ }
+ }
+ if (!matched) {
+ const detailMatch = error.detail
+ ? error.detail.match(/Column (\w+)/)
+ : null;
+
+ const columnName =
+ detailMatch?.[1] ??
+ error.message.match(/ set\s+"([^"]+)"/)?.[1] ??
+ 'unknown';
+ message = `Invalid data type or value for column '${columnName}'.`;
+ _type = DBError.DATA_TYPE_MISMATCH;
+ _extra = { column: columnName };
+ }
+ }
+ break;
+ case '42701':
+ message = 'The column already exists.';
+ if (error.message) {
+ const extractTableNameMatch = error.message.match(
+ / column "(\w+)" of relation "(\w+)" already exists/i,
+ );
+ if (extractTableNameMatch && extractTableNameMatch[1]) {
+ message = `The column '${extractTableNameMatch[1]}' already exists.`;
+ _type = DBError.COLUMN_EXIST;
+ _extra = {
+ column: extractTableNameMatch[1],
+ };
+ }
+ }
+ break;
+ case '42P01':
+ message = 'The table does not exist.';
+ if (error.message) {
+ const extractTableNameMatch = error.message.match(
+ / relation "(\w+)" does not exist/i,
+ );
+ if (extractTableNameMatch && extractTableNameMatch[1]) {
+ message = `The table '${extractTableNameMatch[1]}' does not exist.`;
+ _type = DBError.TABLE_NOT_EXIST;
+ _extra = {
+ table: extractTableNameMatch[1],
+ };
+ }
+
+ const extractColumnNameMatch = error.message.match(
+ / column "(\w+)" does not exist/i,
+ );
+
+ if (extractColumnNameMatch && extractColumnNameMatch[1]) {
+ message = `The column '${extractColumnNameMatch[1]}' does not exist.`;
+ _type = DBError.COLUMN_NOT_EXIST;
+ _extra = {
+ table: extractColumnNameMatch[1],
+ };
+ }
+ }
+ break;
+ case '42703':
+ message = 'The column does not exist.';
+ if (error.message) {
+ const extractTableNameMatch = error.message.match(
+ / column "(\w+)" does not exist/i,
+ );
+ if (extractTableNameMatch && extractTableNameMatch[1]) {
+ message = `The column '${extractTableNameMatch[1]}' does not exist.`;
+ _type = DBError.COLUMN_NOT_EXIST;
+ _extra = {
+ column: extractTableNameMatch[1],
+ };
+ }
+ }
+ break;
+
+ default:
+ return;
+ }
+
+ return {
+ error: NcErrorType.DATABASE_ERROR,
+ message,
+ code: error.code,
+ httpStatus,
+ };
+ }
+}
diff --git a/packages/nocodb/src/helpers/db-error/sqlite.extractor.ts b/packages/nocodb/src/helpers/db-error/sqlite.extractor.ts
new file mode 100644
index 0000000000..b63524c1ba
--- /dev/null
+++ b/packages/nocodb/src/helpers/db-error/sqlite.extractor.ts
@@ -0,0 +1,132 @@
+import { NcErrorType } from 'nocodb-sdk';
+import { DBError } from './utils';
+import type { Logger } from '@nestjs/common';
+import type { DBErrorExtractResult, IClientDbErrorExtractor } from './utils';
+
+export class SqliteDBErrorExtractor implements IClientDbErrorExtractor {
+ constructor(
+ private readonly option?: {
+ dbErrorLogger?: Logger;
+ },
+ ) {}
+
+ extract(error: any): DBErrorExtractResult {
+ if (!error.code) return;
+
+ let message: string;
+ let _extra: Record<string, any>;
+ let _type: DBError;
+ let httpStatus = 422;
+
+ // todo: handle not null constraint error for all databases
+ switch (error.code) {
+ // sqlite errors
+ case 'SQLITE_BUSY':
+ message = 'The database is locked by another process or transaction.';
+ httpStatus = 500;
+ break;
+ case 'SQLITE_CONSTRAINT':
+ {
+ const constraint = /FOREIGN KEY|UNIQUE/.test(error.message)
+ ? error.message.match(/FOREIGN KEY|UNIQUE/gi)?.join(' ')
+ : 'constraint';
+ message = `A ${constraint} constraint was violated: ${error.message}`;
+ _extra = {
+ constraint,
+ };
+ }
+ break;
+ case 'SQLITE_CORRUPT':
+ message = 'The database file is corrupt.';
+ httpStatus = 500;
+ break;
+
+ case 'SQLITE_MISMATCH':
+ if (error.message) {
+ message = 'Data type mismatch in SQLite operation.';
+ _type = DBError.DATA_TYPE_MISMATCH;
+ }
+ break;
+ case 'SQLITE_ERROR':
+ message = 'A SQL error occurred.';
+
+ if (error.message) {
+ const noSuchTableMatch = error.message.match(/no such table: (\w+)/);
+ const tableAlreadyExistsMatch = error.message.match(
+ /SQLITE_ERROR: table `?(\w+)`? already exists/,
+ );
+
+ const duplicateColumnExistsMatch = error.message.match(
+ /SQLITE_ERROR: duplicate column name: (\w+)/,
+ );
+ const unrecognizedTokenMatch = error.message.match(
+ /SQLITE_ERROR: unrecognized token: "(\w+)"/,
+ );
+ const columnDoesNotExistMatch = error.message.match(
+ /SQLITE_ERROR: no such column: (\w+)/,
+ );
+ const constraintFailedMatch = error.message.match(
+ /SQLITE_ERROR: constraint failed: (\w+)/,
+ );
+
+ if (noSuchTableMatch && noSuchTableMatch[1]) {
+ message = `The table '${noSuchTableMatch[1]}' does not exist.`;
+ _type = DBError.TABLE_NOT_EXIST;
+ _extra = {
+ table: noSuchTableMatch[1],
+ };
+ } else if (tableAlreadyExistsMatch && tableAlreadyExistsMatch[1]) {
+ message = `The table '${tableAlreadyExistsMatch[1]}' already exists.`;
+ _type = DBError.TABLE_EXIST;
+ _extra = {
+ table: tableAlreadyExistsMatch[1],
+ };
+ } else if (unrecognizedTokenMatch && unrecognizedTokenMatch[1]) {
+ message = `Unrecognized token: ${unrecognizedTokenMatch[1]}`;
+ _extra = {
+ token: unrecognizedTokenMatch[1],
+ };
+ } else if (columnDoesNotExistMatch && columnDoesNotExistMatch[1]) {
+ message = `The column ${columnDoesNotExistMatch[1]} does not exist.`;
+ _type = DBError.COLUMN_NOT_EXIST;
+ _extra = {
+ column: columnDoesNotExistMatch[1],
+ };
+ } else if (constraintFailedMatch && constraintFailedMatch[1]) {
+ message = `A constraint failed: ${constraintFailedMatch[1]}`;
+ } else if (
+ duplicateColumnExistsMatch &&
+ duplicateColumnExistsMatch[1]
+ ) {
+ message = `The column '${duplicateColumnExistsMatch[1]}' already exists.`;
+ _type = DBError.COLUMN_EXIST;
+ _extra = {
+ column: duplicateColumnExistsMatch[1],
+ };
+ } else {
+ const match = error.message.match(/SQLITE_ERROR:\s*(\w+)/);
+ if (match && match[1]) {
+ message = match[1];
+ }
+ }
+ }
+ break;
+ case 'SQLITE_RANGE':
+ message = 'A column index is out of range.';
+ break;
+ case 'SQLITE_SCHEMA':
+ message = 'The database schema has changed.';
+ break;
+
+ default:
+ return;
+ }
+
+ return {
+ error: NcErrorType.DATABASE_ERROR,
+ message,
+ code: error.code,
+ httpStatus,
+ };
+ }
+}
diff --git a/packages/nocodb/src/helpers/db-error/utils.ts b/packages/nocodb/src/helpers/db-error/utils.ts
new file mode 100644
index 0000000000..eca76619ba
--- /dev/null
+++ b/packages/nocodb/src/helpers/db-error/utils.ts
@@ -0,0 +1,21 @@
+export type DBErrorExtractResult = {
+ message: string;
+ error: string;
+ details?: any;
+ code?: string;
+ httpStatus: number;
+} | void;
+export interface IClientDbErrorExtractor {
+ extract(error: any): DBErrorExtractResult;
+}
+
+export enum DBError {
+ TABLE_EXIST = 'TABLE_EXIST',
+ TABLE_NOT_EXIST = 'TABLE_NOT_EXIST',
+ COLUMN_EXIST = 'COLUMN_EXIST',
+ COLUMN_NOT_EXIST = 'COLUMN_NOT_EXIST',
+ CONSTRAINT_EXIST = 'CONSTRAINT_EXIST',
+ CONSTRAINT_NOT_EXIST = 'CONSTRAINT_NOT_EXIST',
+ COLUMN_NOT_NULL = 'COLUMN_NOT_NULL',
+ DATA_TYPE_MISMATCH = 'DATA_TYPE_MISMATCH',
+}
diff --git a/packages/nocodb/tests/unit/error/db-error-extractor/pg-error-extractor.test.ts b/packages/nocodb/tests/unit/error/db-error-extractor/pg-error-extractor.test.ts
new file mode 100644
index 0000000000..5a0e4cb83c
--- /dev/null
+++ b/packages/nocodb/tests/unit/error/db-error-extractor/pg-error-extractor.test.ts
@@ -0,0 +1,88 @@
+import { expect } from 'chai';
+import 'mocha';
+import { ClientType } from 'nocodb-sdk';
+// using formula init for now
+import { BaseModelSqlv2 } from '~/db/BaseModelSqlv2';
+import { DBErrorExtractor } from '~/helpers/db-error/extractor';
+import NcConnectionMgrv2 from '~/utils/common/NcConnectionMgrv2';
+import { Source } from '../../../../src/models';
+import { initInitialModel } from '../../formula/initModel';
+
+function pgErrorExtractorTests() {
+ let _setup;
+ let _context;
+ let _ctx: {
+ workspace_id: string;
+ base_id: string;
+ };
+ let _base;
+ let _tables;
+ let _view;
+ let baseModelSql: BaseModelSqlv2;
+
+ beforeEach(async function () {
+ const setup = await initInitialModel();
+ _setup = setup;
+ _context = setup.context;
+ _ctx = setup.ctx;
+ _base = setup.base;
+ _tables = setup.tables;
+
+ const view = await setup.tables.table1.getViews(setup.ctx)[0];
+
+ const source = await Source.get(setup.ctx, setup.tables.table1.source_id);
+ baseModelSql = new BaseModelSqlv2({
+ dbDriver: await NcConnectionMgrv2.get(source),
+ model: setup.tables.table1,
+ view,
+ context: setup.ctx,
+ });
+ });
+
+ const clientType = ClientType.PG;
+ it(`will extract pg syntax error`, async () => {
+ const knex = baseModelSql.dbDriver;
+ const columnTitle = (await _tables.table1.getColumns()).find(
+ (col) => col.title === 'Title',
+ );
+ try {
+ await knex(_tables.table1.table_name).select(
+ knex.raw(`SUBSTRING (?, 0, -1) as `, [
+ `${_tables.table1.table_name}.${columnTitle.column_name}`,
+ ]),
+ );
+ } catch (ex) {
+ const extractResult = DBErrorExtractor.get().extractDbError(ex, {
+ clientType,
+ });
+ expect(extractResult.message).to.satisfy((msg) =>
+ msg.startsWith('There was a syntax error in your SQL query'),
+ );
+ }
+ });
+
+ it(`will extract pg substring negative length error`, async () => {
+ const knex = baseModelSql.dbDriver;
+ const columnTitle = (await _tables.table1.getColumns()).find(
+ (col) => col.title === 'Title',
+ );
+ try {
+ await knex(_tables.table1.table_name).select(
+ knex.raw(`SUBSTRING (?, 0, -1) as field`, [
+ `${_tables.table1.table_name}.${columnTitle.column_name}`,
+ ]),
+ );
+ } catch (ex) {
+ const extractResult = DBErrorExtractor.get().extractDbError(ex, {
+ clientType,
+ });
+ expect(extractResult.message).to.satisfy((msg) =>
+ msg.startsWith('Negative substring length not allowed'),
+ );
+ }
+ });
+}
+
+export function pgErrorExtractorTest() {
+ describe('PgErrorExtractorTest', pgErrorExtractorTests);
+}
diff --git a/packages/nocodb/tests/unit/error/index.test.ts b/packages/nocodb/tests/unit/error/index.test.ts
new file mode 100644
index 0000000000..3a27e206a3
--- /dev/null
+++ b/packages/nocodb/tests/unit/error/index.test.ts
@@ -0,0 +1,10 @@
+import 'mocha';
+import { pgErrorExtractorTest } from './db-error-extractor/pg-error-extractor.test';
+
+function _errorTests() {
+ pgErrorExtractorTest();
+}
+
+export function errorTests() {
+ describe('Error', _errorTests);
+}
diff --git a/packages/nocodb/tests/unit/index.test.ts b/packages/nocodb/tests/unit/index.test.ts
index c8e4fa453c..6919e18f65 100644
--- a/packages/nocodb/tests/unit/index.test.ts
+++ b/packages/nocodb/tests/unit/index.test.ts
@@ -6,6 +6,7 @@ import { formulaTests } from './formula/index.test';
import TestDbMngr from './TestDbMngr';
import { dataApiV3Test } from './rest/tests/dataApiV3/index.test';
import { processorTests } from './processor/index.test';
+import { errorTests } from './error/index.test';
process.env.NODE_ENV = 'test';
process.env.TEST = 'true';
@@ -22,6 +23,7 @@ dotenv.config({
modelTests();
formulaTests();
+ errorTests();
restTests();
processorTests();
await dataApiV3Test();