diff --git a/packages/adapter-libsql/src/errors.test.ts b/packages/adapter-libsql/src/errors.test.ts index 14d5b0e8f855..c139987c4e0c 100644 --- a/packages/adapter-libsql/src/errors.test.ts +++ b/packages/adapter-libsql/src/errors.test.ts @@ -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', diff --git a/packages/adapter-libsql/src/errors.ts b/packages/adapter-libsql/src/errors.ts index 0e400590bd44..fc8d4bd353d2 100644 --- a/packages/adapter-libsql/src/errors.ts +++ b/packages/adapter-libsql/src/errors.ts @@ -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: { @@ -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] +}