Skip to content

Commit 577b098

Browse files
committed
fix(server): include Bun SQLite error codes in diagnostics
Handle Bun errno separately from the Node SQLite fields. Test a real Node SQL failure and wrapped Bun errors without copying query data.
1 parent b7f2f55 commit 577b098

2 files changed

Lines changed: 64 additions & 33 deletions

File tree

‎apps/server/src/persistence/Errors.test.ts‎

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { assert, it } from "@effect/vitest";
2+
import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
23
import * as Effect from "effect/Effect";
34
import * as Schema from "effect/Schema";
5+
import * as SqlClient from "effect/unstable/sql/SqlClient";
6+
import { classifySqliteError, SqlError } from "effect/unstable/sql/SqlError";
47

58
import { PersistenceDecodeError, PersistenceSqlError, toPersistenceSqlError } from "./Errors.ts";
69

@@ -25,16 +28,23 @@ it("keeps SQL operation context without a tautological detail", () => {
2528
assert.equal(error.message, "SQL error in AuthSessionRepository.list:query");
2629
});
2730

28-
it("names the SQLite condition by its normalized result code", () => {
29-
const cause = Object.assign(new Error("UNIQUE constraint failed: orders.customer_email"), {
30-
errcode: 1555,
31-
errstr: "constraint failed",
32-
});
33-
const error = toPersistenceSqlError("OrchestrationCommandReceiptRepository.upsert:query")(cause);
31+
it.effect("names a real SQLite condition without copying query data", () =>
32+
Effect.gen(function* () {
33+
const sql = yield* SqlClient.SqlClient;
34+
const payload = "sql-private-sentinel";
35+
yield* sql`CREATE TABLE error_test (private_column TEXT PRIMARY KEY)`;
36+
yield* sql`INSERT INTO error_test VALUES (${payload})`;
37+
const cause = yield* Effect.flip(sql`INSERT INTO error_test VALUES (${payload})`);
38+
const error = toPersistenceSqlError("OrchestrationCommandReceiptRepository.upsert:query")(
39+
cause,
40+
);
3441

35-
assert.equal(error.detail, "SQLITE(1555) constraint failed");
36-
assert.equal(error.cause, cause);
37-
});
42+
assert.equal(error.detail, "SQLITE(1555) constraint failed");
43+
assert.equal(error.cause, cause);
44+
assert.notInclude(error.message, payload);
45+
assert.notInclude(error.message, "private_column");
46+
}).pipe(Effect.provide(NodeSqliteClient.layerMemory())),
47+
);
3848

3949
it("reads the condition through a wrapping driver error", () => {
4050
const driver = Object.assign(new Error("locked"), { errcode: 5, errstr: "database is locked" });
@@ -45,18 +55,29 @@ it("reads the condition through a wrapping driver error", () => {
4555
assert.equal(error.detail, "SQLITE(5) database is locked");
4656
});
4757

48-
it("keeps the driver's own prose out of the message", () => {
49-
const cause = Object.assign(new Error("UNIQUE constraint failed: orders.customer_email"), {
50-
errcode: 1555,
51-
errstr: "constraint failed",
52-
});
53-
const error = toPersistenceSqlError("AuthSessionRepository.create:query")(cause);
54-
55-
assert.ok(!error.message.includes("customer_email"));
56-
});
58+
it.each([{ errno: 1555, code: "SQLITE_CONSTRAINT_PRIMARYKEY" }, { errno: 1 }])(
59+
"names Bun SQLite condition $errno through the SQL error wrapper",
60+
(condition) => {
61+
const driver = Object.assign(new Error("bun-sql-private-sentinel"), {
62+
name: "SQLiteError",
63+
...condition,
64+
});
65+
const cause = new SqlError({ reason: classifySqliteError(driver) });
66+
const error = toPersistenceSqlError("AuthSessionRepository.create:query")(cause);
67+
68+
assert.equal(
69+
error.message,
70+
`SQL error in AuthSessionRepository.create:query: SQLITE(${condition.errno})`,
71+
);
72+
assert.equal(error.cause, cause);
73+
},
74+
);
5775

58-
it("omits a detail for a cause it cannot categorize", () => {
59-
const error = toPersistenceSqlError("AuthSessionRepository.list:query")(new Error("unhelpful"));
76+
it.each([
77+
new Error("unhelpful"),
78+
Object.assign(new Error("file not found"), { errno: -2, code: "ENOENT" }),
79+
])("omits a detail for a cause it cannot categorize (%#)", (cause) => {
80+
const error = toPersistenceSqlError("AuthSessionRepository.list:query")(cause);
6081

6182
assert.equal(error.detail, undefined);
6283
assert.equal(error.message, "SQL error in AuthSessionRepository.list:query");

‎apps/server/src/persistence/Errors.ts‎

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as Predicate from "effect/Predicate";
12
import * as Schema from "effect/Schema";
23
import * as SchemaIssue from "effect/SchemaIssue";
34

@@ -72,21 +73,31 @@ export class PersistenceDecodeError extends Schema.TaggedErrorClass<PersistenceD
7273
const isPersistenceSqlError = Schema.is(PersistenceSqlError);
7374
const isPersistenceDecodeError = Schema.is(PersistenceDecodeError);
7475

75-
// Kept for orchestration/projection call sites, which are being revamped separately.
7676
/**
77-
* SQLite names its condition through a fixed result-code table, so the code and
78-
* its canonical string are bounded and carry no query data. The driver message
79-
* is not bounded: a constraint failure spells out the table and column it hit.
80-
* Only the normalized pair is carried here; `cause` keeps everything else.
77+
* Read a SQLite condition through SQL error wrappers.
78+
* Use Node's fixed description or Bun's numeric code, never the driver message.
8179
*/
8280
function sqliteCondition(cause: unknown): string | undefined {
83-
let value: unknown = cause;
84-
for (let depth = 0; depth < 4 && typeof value === "object" && value !== null; depth += 1) {
85-
const { errcode, errstr } = value as { errcode?: unknown; errstr?: unknown };
86-
if (typeof errcode === "number" && typeof errstr === "string") {
87-
return `SQLITE(${errcode}) ${errstr}`;
81+
let value = cause;
82+
for (let depth = 0; depth < 4 && Predicate.isObject(value); depth += 1) {
83+
if (
84+
"errcode" in value &&
85+
typeof value.errcode === "number" &&
86+
"errstr" in value &&
87+
typeof value.errstr === "string"
88+
) {
89+
return `SQLITE(${value.errcode}) ${value.errstr}`;
90+
}
91+
if (
92+
"name" in value &&
93+
value.name === "SQLiteError" &&
94+
"errno" in value &&
95+
typeof value.errno === "number" &&
96+
Number.isInteger(value.errno)
97+
) {
98+
return `SQLITE(${value.errno})`;
8899
}
89-
value = (value as { cause?: unknown }).cause;
100+
value = "cause" in value ? value.cause : undefined;
90101
}
91102
return undefined;
92103
}
@@ -101,10 +112,9 @@ function describeSqlCause(cause: unknown): string | undefined {
101112
return sqliteCondition(cause);
102113
}
103114

115+
// Kept for orchestration/projection call sites, which are being revamped separately.
104116
export function toPersistenceSqlError(operation: string) {
105117
return (cause: unknown): PersistenceSqlError => {
106-
// Restating the operation as the detail only doubled it in the message and
107-
// buried the cause; `message` already reads well without a detail.
108118
const detail = describeSqlCause(cause);
109119
return new PersistenceSqlError({
110120
operation,

0 commit comments

Comments
 (0)