Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions packages/adapter-libsql/src/errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,60 @@ describe('LibSQL error handling', () => {
})
})

describe('remote errors without a numeric code', () => {
const remote = (message: string) => ({
code: 'SQLITE_CONSTRAINT',
message: `SQLITE_CONSTRAINT: SQLITE_CONSTRAINT: SQLite error: ${message}`,
cause: { code: 'SQLITE_CONSTRAINT' },
})

test.each([
[
'unique',
'UNIQUE constraint failed: User.email',
{ kind: 'UniqueConstraintViolation', constraint: { fields: ['email'] }, table: 'User' },
],
[
'primary key',
'UNIQUE constraint failed: User.id',
{ kind: 'UniqueConstraintViolation', constraint: { fields: ['id'] }, table: 'User' },
],
[
'not null',
'NOT NULL constraint failed: User.name',
{ kind: 'NullConstraintViolation', constraint: { fields: ['name'] } },
],
[
'not null on a column named like another constraint',
'NOT NULL constraint failed: User.UNIQUE constraint failed',
{ kind: 'NullConstraintViolation', constraint: { fields: ['UNIQUE constraint failed'] } },
],
[
'foreign key',
'FOREIGN KEY constraint failed',
{ kind: 'ForeignKeyConstraintViolation', constraint: { foreignKey: {} } },
],
])('%s violation is mapped from the message', (_name, message, expected) => {
const error = remote(message)
expect(convertDriverError(error)).toEqual({
...expected,
originalCode: undefined,
originalMessage: error.message,
})
})

test('other remote errors keep the generic code', () => {
const error = { code: 'SQLITE_ERROR', message: 'SQLITE_ERROR: SQLite error: near "SELEC": syntax error' }
expect(convertDriverError(error)).toEqual({
kind: 'sqlite',
extendedCode: 1,
message: error.message,
originalCode: undefined,
originalMessage: error.message,
})
})
})

test('unique constraint violation reports the fields and the table', () => {
const dbError = convertDriverError({
code: 'SQLITE_CONSTRAINT_UNIQUE',
Expand Down
25 changes: 24 additions & 1 deletion packages/adapter-libsql/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function convertDriverError(error: unknown): DriverAdapterErrorObject {
}

export function mapDriverError(error: LibsqlError): MappedError {
const rawCode: number = error.rawCode ?? error.cause?.['rawCode'] ?? 1
const rawCode: number = error.rawCode ?? error.cause?.['rawCode'] ?? constraintCodeFromMessage(error) ?? 1
switch (rawCode) {
case 2067:
case 1555: {
Expand Down Expand Up @@ -86,3 +86,26 @@ function isDriverError(error: any): error is LibsqlError {
(typeof error.rawCode === 'number' || error.rawCode === undefined)
)
}

const CONSTRAINT_CODES_BY_MESSAGE: [header: string, extendedCode: number][] = [
['UNIQUE constraint failed', 2067],
['PRIMARY KEY constraint failed', 1555],
['NOT NULL constraint failed', 1299],
['FOREIGN KEY constraint failed', 787],
]

const REMOTE_ERROR_PREFIX = /^(?:[A-Z_]+: )*(?:SQLite error: )?/

/**
* Errors from a remote libSQL server carry only the primary `SQLITE_CONSTRAINT`
* code and no numeric `rawCode`, so the extended code is recovered from
* SQLite's message. Only the start of the message is matched, since the table
* and column names that follow it can contain any text.
*/
function constraintCodeFromMessage(error: LibsqlError): number | undefined {
if (!error.code.startsWith('SQLITE_CONSTRAINT')) {
return undefined
}
const message = error.message.replace(REMOTE_ERROR_PREFIX, '')
return CONSTRAINT_CODES_BY_MESSAGE.find(([header]) => message.startsWith(header))?.[1]
}
Loading