Skip to content

@effect/sql-d1: synchronous prepare failure is a defect on .raw/.unprepared but a typed SqlError on cached queries #10864

Done

Description

@hyponex

What version of Effect is running?

effect 4.1.5 / @effect/sql-d1 4.1.5 (latest). The same code is on main (packages/sql/d1/src/D1Client.ts).

What steps can reproduce the bug?

When the D1 binding's prepare throws synchronously, cached queries and .values fail with a typed SqlError, but the same query through .raw or .unprepared dies with a defect.

import { Effect } from "effect";
import { SqlClient } from "effect/sql/SqlClient";
import * as D1Client from "@effect/sql-d1/D1Client";

const problem = new Error("synthetic D1 prepare fault");
const db = { prepare() { throw problem; } };
const cases = {
cached: (sql) => sql.unsafe("select 1"),
values: (sql) => sql.unsafe("select 1").values,
raw: (sql) => sql.unsafe("select 1").raw,
unprepared: (sql) => sql.unsafe("select 1").unprepared,
};
for (const [name, run] of Object.entries(cases)) {
const exit = await Effect.runPromiseExit(
Effect.flatMap(SqlClient, run).pipe(Effect.provide(D1Client.layer({ db }))),
);
console.log(name, exit.cause.reasons.map((r) => r._tag));
}
Output:

cached [ 'Fail' ]
values [ 'Fail' ]
raw [ 'Die' ]
unprepared [ 'Die' ]
The binding is intentionally minimal (only prepare); no real SQL executes.

What is the expected behavior?

All four paths fail the same way: a typed SqlError whose cause is the original exception.

What do you see instead?

cached / values: Fail with SqlError ("Failed to prepare statement"), cause = original error
raw / unprepared: Die, defect = original error

Additional information

The cached paths prepare through the cache lookup, which wraps db.prepare(sql) in Effect.try. runRaw and runUncached instead call db.prepare(sql) eagerly and pass the result to runStatementRaw / runStatement, so a synchronous throw escapes the typed error boundary:

const runRaw = (sql, params = []) => runStatementRaw(db.prepare(sql), params)
const runUncached = (sql, params = []) => runStatement(db.prepare(sql), params)
Suggestion: prepare inside Effect.try with the same SqlError mapping the cache lookup uses, then flatMap into runStatementRaw / runStatement.

Activity

  1. added
    bug
    on Nov 23, 2026
  2. self-assigned this
    on Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions