Skip to content

Commit

Permalink
fix(adapter-d1): map more SQLite errors (prisma#23504)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jolg42 authored Mar 15, 2024
1 parent efb7a28 commit ed2f2fc
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions packages/adapter-d1/src/d1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,25 +93,37 @@ class D1Queryable<ClientT extends StdClient> implements Queryable {
return ok([columnNames, rows])
}
} catch (e) {
const error = e as Error
console.error('Error in performIO: %O', error)
console.error('Error in performIO: %O', e)
const { message } = e

// We only get the error message, not the error code.
// "name":"Error","message":"D1_ERROR: UNIQUE constraint failed: User.email"
// So we try to match some errors and use the generic error code as a fallback.
// https://www.sqlite.org/rescode.html
// 1 = The SQLITE_ERROR result code is a generic error code that is used when no other more specific error code is available.
// See quaint https://github.com/prisma/prisma-engines/blob/main/quaint/src/connector/sqlite/error.rs
// some errors are matched by the extended code and others by the message there.
let extendedCode = 1
if (error.message.startsWith('D1_ERROR: UNIQUE constraint failed:')) {
if (message.startsWith('D1_ERROR: UNIQUE constraint failed:')) {
extendedCode = 2067
} else if (error.message.startsWith('D1_ERROR: FOREIGN KEY constraint failed')) {
} else if (message.startsWith('D1_ERROR: FOREIGN KEY constraint failed')) {
extendedCode = 787
} else if (message.startsWith('D1_ERROR: NOT NULL constraint failed')) {
extendedCode = 1299
}
// These below were added based on
// https://github.com/prisma/prisma-engines/blob/main/quaint/src/connector/sqlite/error.rs
// https://github.com/prisma/prisma-engines/blob/main/quaint/src/connector/sqlite/ffi.rs
else if (message.startsWith('D1_ERROR: CHECK constraint failed')) {
extendedCode = 1811
} else if (message.startsWith('D1_ERROR: PRIMARY KEY constraint failed')) {
extendedCode = 1555
}

return err({
kind: 'Sqlite',
extendedCode,
message: error.message,
message,
})
}
}
Expand Down

0 comments on commit ed2f2fc

Please sign in to comment.