From 49d3c99bd88301bbf581eaed356dd0653becec3f Mon Sep 17 00:00:00 2001 From: effect-bot <129879685+effect-bot@users.noreply.github.com> Date: Mon, 23 Nov 2026 18:19:59 -0500 Subject: [PATCH] fix(sql-d1): return typed SqlError when prepare throws on raw and unprepared queries (#10867) Co-authored-by: Satoru Mikawa <6104306+hyponex@users.noreply.github.com> --- .changeset/d1-prepare-sql-error.md | 5 +++++ packages/sql/d1/src/D1Client.ts | 16 +++++++++------- packages/sql/d1/test/Client.test.ts | 22 ++++++++++++++++++++++ 3 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 .changeset/d1-prepare-sql-error.md diff --git a/.changeset/d1-prepare-sql-error.md b/.changeset/d1-prepare-sql-error.md new file mode 100644 index 0000000000..216ec39ea3 --- /dev/null +++ b/.changeset/d1-prepare-sql-error.md @@ -0,0 +1,5 @@ +--- +"@effect/sql-d1": patch +--- + +Return a typed `SqlError` instead of dying when `D1Database.prepare()` throws synchronously on `.raw` and `.unprepared` queries. diff --git a/packages/sql/d1/src/D1Client.ts b/packages/sql/d1/src/D1Client.ts index 56e84d3e9e..c5e4f85a40 100644 --- a/packages/sql/d1/src/D1Client.ts +++ b/packages/sql/d1/src/D1Client.ts @@ -228,14 +228,16 @@ export const make = ( const makeConnection = Effect.gen(function*() { const db = options.db + const prepare = (sql: string) => + Effect.try({ + try: () => db.prepare(sql), + catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to prepare statement", "prepare") }) + }) + const prepareCache = yield* Cache.make({ capacity: options.prepareCacheSize ?? 200, timeToLive: options.prepareCacheTTL ?? Duration.minutes(10), - lookup: (sql: string) => - Effect.try({ - try: () => db.prepare(sql), - catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to prepare statement", "prepare") }) - }) + lookup: prepare }) const runStatementRaw = ( @@ -262,7 +264,7 @@ export const make = ( const runRaw = ( sql: string, params: ReadonlyArray = [] - ) => runStatementRaw(db.prepare(sql), params) + ) => Effect.flatMap(prepare(sql), (s) => runStatementRaw(s, params)) const runCached = ( sql: string, @@ -272,7 +274,7 @@ export const make = ( const runUncached = ( sql: string, params: ReadonlyArray = [] - ) => runStatement(db.prepare(sql), params) + ) => Effect.flatMap(prepare(sql), (s) => runStatement(s, params)) const runValues = ( sql: string, diff --git a/packages/sql/d1/test/Client.test.ts b/packages/sql/d1/test/Client.test.ts index b31c71b623..a94c28168a 100644 --- a/packages/sql/d1/test/Client.test.ts +++ b/packages/sql/d1/test/Client.test.ts @@ -40,6 +40,28 @@ describe("Client", { concurrent: false, timeout: 30_000 }, () => { Effect.provide(Reactivity.layer) )) + it.effect("fails with SqlError when prepare throws on raw and unprepared queries", () => + Effect.gen(function*() { + const cause = new Error("prepare failed") + const failingDb = { + prepare: () => { + throw cause + } + } as any + + const client = yield* D1Client.make({ db: failingDb }) + + for (const query of [client`SELECT 1`.raw, client`SELECT 1`.unprepared]) { + const error = yield* Effect.flip(query) + assert.strictEqual(error.reason._tag, "UnknownError") + assert.strictEqual(error.reason.operation, "prepare") + assert.strictEqual(error.reason.cause, cause) + } + }).pipe( + Effect.scoped, + Effect.provide(Reactivity.layer) + )) + it.effect("should handle queries without transactions", () => Effect.gen(function*() { const sql = yield* D1Client.D1Client