@ -15,6 +15,7 @@ export enum DBError {
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
@ -49,6 +50,13 @@ export function extractDBError(error): {
case 'SQLITE_CORRUPT' :
message = 'The database file is corrupt.' ;
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.' ;
@ -121,6 +129,26 @@ export function extractDBError(error): {
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.' ;
@ -273,6 +301,31 @@ export function extractDBError(error): {
}
}
break ;
case '22P02' : // PostgreSQL invalid_text_representation
case '22003' : // PostgreSQL numeric_value_out_of_range
if ( error . message ) {
const pgTypeMismatchMatch = error . message . match (
/invalid input syntax for (\w+): "(.+)"(?: in column "(\w+)")?/i ,
) ;
if ( pgTypeMismatchMatch ) {
const dataType = pgTypeMismatchMatch [ 1 ] ;
const invalidValue = pgTypeMismatchMatch [ 2 ] ;
const columnName = pgTypeMismatchMatch [ 3 ] || 'unknown' ;
message = ` Invalid ${ dataType } value ' ${ invalidValue } ' for column ' ${ columnName } ' ` ;
_type = DBError . DATA_TYPE_MISMATCH ;
_extra = { dataType , column : columnName , value : invalidValue } ;
} else {
const detailMatch = error . detail
? error . detail . match ( /Column (\w+)/ )
: null ;
const columnName = detailMatch ? detailMatch [ 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 ) {