fix(server): include SQLite conditions in persistence errors
Include SQLite conditions and schema issue tags without copying query data. Continue @Sy-D's [#4837](https://github.com/pingdotgg/t3code/pull/4837). Add the missing Bun error codes and test the real SQL client. Created with GPT-6 Astra (preview) in Codex. Co-authored-by: Sy-D <[email protected]> Co-authored-by: Claude Opus 5 <[email protected]>
This commit is contained in:
2 files changed
+117
-4
No files matched your search
@@ -1,8 +1,11 @@
|
||||
import { assert, it } from "@effect/vitest";
|
||||
import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Schema from "effect/Schema";
|
||||
import * as SqlClient from "effect/unstable/sql/SqlClient";
|
||||
import { classifySqliteError, SqlError } from "effect/unstable/sql/SqlError";
|
||||
|
||||
import { PersistenceDecodeError, PersistenceSqlError } from "./Errors.ts";
|
||||
import { PersistenceDecodeError, PersistenceSqlError, toPersistenceSqlError } from "./Errors.ts";
|
||||
|
||||
const decodeRuntimePayload = Schema.decodeUnknownEffect(
|
||||
Schema.Struct({
|
||||
@@ -25,6 +28,74 @@ it("keeps SQL operation context without a tautological detail", () => {
|
||||
assert.equal(error.message, "SQL error in AuthSessionRepository.list:query");
|
||||
});
|
||||
|
||||
it.effect("names a real SQLite condition without copying query data", () =>
|
||||
Effect.gen(function* () {
|
||||
const sql = yield* SqlClient.SqlClient;
|
||||
const payload = "sql-private-sentinel";
|
||||
yield* sql`CREATE TABLE error_test (private_column TEXT PRIMARY KEY)`;
|
||||
yield* sql`INSERT INTO error_test VALUES (${payload})`;
|
||||
const cause = yield* Effect.flip(sql`INSERT INTO error_test VALUES (${payload})`);
|
||||
const error = toPersistenceSqlError("OrchestrationCommandReceiptRepository.upsert:query")(
|
||||
cause,
|
||||
);
|
||||
|
||||
assert.equal(error.detail, "SQLITE(1555) constraint failed");
|
||||
assert.equal(error.cause, cause);
|
||||
assert.notInclude(error.message, payload);
|
||||
assert.notInclude(error.message, "private_column");
|
||||
}).pipe(Effect.provide(NodeSqliteClient.layerMemory())),
|
||||
);
|
||||
|
||||
it("reads the condition through a wrapping driver error", () => {
|
||||
const driver = Object.assign(new Error("locked"), { errcode: 5, errstr: "database is locked" });
|
||||
const error = toPersistenceSqlError("AuthSessionRepository.list:query")(
|
||||
new Error("Failed to prepare statement", { cause: driver }),
|
||||
);
|
||||
|
||||
assert.equal(error.detail, "SQLITE(5) database is locked");
|
||||
});
|
||||
|
||||
it.each([{ errno: 1555, code: "SQLITE_CONSTRAINT_PRIMARYKEY" }, { errno: 1 }])(
|
||||
"names Bun SQLite condition $errno through the SQL error wrapper",
|
||||
(condition) => {
|
||||
const driver = Object.assign(new Error("bun-sql-private-sentinel"), {
|
||||
name: "SQLiteError",
|
||||
...condition,
|
||||
});
|
||||
const cause = new SqlError({ reason: classifySqliteError(driver) });
|
||||
const error = toPersistenceSqlError("AuthSessionRepository.create:query")(cause);
|
||||
|
||||
assert.equal(
|
||||
error.message,
|
||||
`SQL error in AuthSessionRepository.create:query: SQLITE(${condition.errno})`,
|
||||
);
|
||||
assert.equal(error.cause, cause);
|
||||
},
|
||||
);
|
||||
|
||||
it.each([
|
||||
new Error("unhelpful"),
|
||||
Object.assign(new Error("file not found"), { errno: -2, code: "ENOENT" }),
|
||||
])("omits a detail for a cause it cannot categorize (%#)", (cause) => {
|
||||
const error = toPersistenceSqlError("AuthSessionRepository.list:query")(cause);
|
||||
|
||||
assert.equal(error.detail, undefined);
|
||||
assert.equal(error.message, "SQL error in AuthSessionRepository.list:query");
|
||||
});
|
||||
|
||||
it.effect("summarizes a schema cause by issue tag instead of by rejected value", () =>
|
||||
Effect.gen(function* () {
|
||||
const rejectedPayload = "sql-mapper-secret-sentinel";
|
||||
const cause = yield* Effect.flip(
|
||||
decodeRuntimePayload({ runtimePayload: { attempt: rejectedPayload } }),
|
||||
);
|
||||
const error = toPersistenceSqlError("ProviderSessionRuntimeRepository.list:query")(cause);
|
||||
|
||||
assert.ok(error.detail !== undefined);
|
||||
assert.ok(!error.message.includes(rejectedPayload));
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("maps schema errors without copying rejected payloads into diagnostics", () =>
|
||||
Effect.gen(function* () {
|
||||
const rejectedPayload = "runtime-payload-secret-sentinel";
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import * as Predicate from "effect/Predicate";
|
||||
import * as Schema from "effect/Schema";
|
||||
import * as SchemaIssue from "effect/SchemaIssue";
|
||||
|
||||
@@ -72,14 +73,55 @@ export class PersistenceDecodeError extends Schema.TaggedErrorClass<PersistenceD
|
||||
const isPersistenceSqlError = Schema.is(PersistenceSqlError);
|
||||
const isPersistenceDecodeError = Schema.is(PersistenceDecodeError);
|
||||
|
||||
/**
|
||||
* Read a SQLite condition through SQL error wrappers.
|
||||
* Use Node's fixed description or Bun's numeric code, never the driver message.
|
||||
*/
|
||||
function sqliteCondition(cause: unknown): string | undefined {
|
||||
let value = cause;
|
||||
for (let depth = 0; depth < 4 && Predicate.isObject(value); depth += 1) {
|
||||
if (
|
||||
"errcode" in value &&
|
||||
typeof value.errcode === "number" &&
|
||||
"errstr" in value &&
|
||||
typeof value.errstr === "string"
|
||||
) {
|
||||
return `SQLITE(${value.errcode}) ${value.errstr}`;
|
||||
}
|
||||
if (
|
||||
"name" in value &&
|
||||
value.name === "SQLiteError" &&
|
||||
"errno" in value &&
|
||||
typeof value.errno === "number" &&
|
||||
Number.isInteger(value.errno)
|
||||
) {
|
||||
return `SQLITE(${value.errno})`;
|
||||
}
|
||||
value = "cause" in value ? value.cause : undefined;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* A rejected payload must never reach diagnostics, so a schema failure
|
||||
* contributes only its issue tags, and a driver failure only its normalized
|
||||
* condition. Anything the mapper cannot categorize leaves the detail unset.
|
||||
*/
|
||||
function describeSqlCause(cause: unknown): string | undefined {
|
||||
if (Schema.isSchemaError(cause)) return summarizeSchemaIssue(cause.issue);
|
||||
return sqliteCondition(cause);
|
||||
}
|
||||
|
||||
// Kept for orchestration/projection call sites, which are being revamped separately.
|
||||
export function toPersistenceSqlError(operation: string) {
|
||||
return (cause: unknown): PersistenceSqlError =>
|
||||
new PersistenceSqlError({
|
||||
return (cause: unknown): PersistenceSqlError => {
|
||||
const detail = describeSqlCause(cause);
|
||||
return new PersistenceSqlError({
|
||||
operation,
|
||||
detail: `Failed to execute ${operation}`,
|
||||
...(detail === undefined ? {} : { detail }),
|
||||
cause,
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
// Kept for orchestration/projection call sites, which are being revamped separately.
|
||||
|
||||
Reference in new issue
Block a user