import { ContainsNullValuesException } from '../contains-null-values' ; import { InvalidForeignKeyException } from '../invalid-foreign-key' ; import { NotNullViolationException } from '../not-null-violation' ; import { RecordNotUniqueException } from '../record-not-unique' ; import { SQLiteError } from './types' ; export function extractError ( error ) { if ( error . message . includes ( 'SQLITE_CONSTRAINT: NOT NULL' ) ) { return notNullConstraint ( error ) ; } if ( error . message . includes ( 'SQLITE_CONSTRAINT: UNIQUE' ) ) { const errorParts = error . message . split ( ' ' ) ; const [ table , column ] = errorParts [ errorParts . length - 1 ] . split ( '.' ) ; if ( ! table || ! column ) return error ; return new RecordNotUniqueException ( column , { collection : table , field : column , } ) ; } if ( error . message . includes ( 'SQLITE_CONSTRAINT: FOREIGN KEY' ) ) { /**
         * NOTE:
         * SQLite doesn't return any useful information in it's foreign key constraint failed error, so
         * we can't extract the table/column/value accurately
         */ return new InvalidForeignKeyException ( null ) ; } return error ; } function notNullConstraint ( error ) { const errorParts = error . message . split ( ' ' ) ; const [ table , column ] = errorParts [ errorParts . length - 1 ] . split ( '.' ) ; if ( table && column ) { if ( table . startsWith ( '_knex_temp_alter' ) ) { return new ContainsNullValuesException ( column ) ; } return new NotNullViolationException ( column , { collection : table , field : column , } ) ; } return error ; }