| ... | @@ -1,8 +1,11 @@ | ... | @@ -1,8 +1,11 @@ |
| 1 | import { assert, it } from "@effect/vitest"; | 1 | import { assert, it } from "@effect/vitest"; |
| | 2 | import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient"; |
| 2 | import * as Effect from "effect/Effect"; | 3 | import * as Effect from "effect/Effect"; |
| 3 | import * as Schema from "effect/Schema"; | 4 | import * as Schema from "effect/Schema"; |
| | 5 | import * as SqlClient from "effect/unstable/sql/SqlClient"; |
| | 6 | import { classifySqliteError, SqlError } from "effect/unstable/sql/SqlError"; |
| 4 | | 7 | |
| 5 | import { PersistenceDecodeError, PersistenceSqlError } from "./Errors.ts"; | 8 | import { PersistenceDecodeError, PersistenceSqlError, toPersistenceSqlError } from "./Errors.ts"; |
| 6 | | 9 | |
| 7 | const decodeRuntimePayload = Schema.decodeUnknownEffect( | 10 | const decodeRuntimePayload = Schema.decodeUnknownEffect( |
| 8 | Schema.Struct({ | 11 | Schema.Struct({ |
| ... | @@ -25,6 +28,74 @@ it("keeps SQL operation context without a tautological detail", () => { | ... | @@ -25,6 +28,74 @@ it("keeps SQL operation context without a tautological detail", () => { |
| 25 | assert.equal(error.message, "SQL error in AuthSessionRepository.list:query"); | 28 | assert.equal(error.message, "SQL error in AuthSessionRepository.list:query"); |
| 26 | }); | 29 | }); |
| 27 | | 30 | |
| | 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 | ); |
| | 41 | |
| | 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 | ); |
| | 48 | |
| | 49 | it("reads the condition through a wrapping driver error", () => { |
| | 50 | const driver = Object.assign(new Error("locked"), { errcode: 5, errstr: "database is locked" }); |
| | 51 | const error = toPersistenceSqlError("AuthSessionRepository.list:query")( |
| | 52 | new Error("Failed to prepare statement", { cause: driver }), |
| | 53 | ); |
| | 54 | |
| | 55 | assert.equal(error.detail, "SQLITE(5) database is locked"); |
| | 56 | }); |
| | 57 | |
| | 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 | ); |
| | 75 | |
| | 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); |
| | 81 | |
| | 82 | assert.equal(error.detail, undefined); |
| | 83 | assert.equal(error.message, "SQL error in AuthSessionRepository.list:query"); |
| | 84 | }); |
| | 85 | |
| | 86 | it.effect("summarizes a schema cause by issue tag instead of by rejected value", () => |
| | 87 | Effect.gen(function* () { |
| | 88 | const rejectedPayload = "sql-mapper-secret-sentinel"; |
| | 89 | const cause = yield* Effect.flip( |
| | 90 | decodeRuntimePayload({ runtimePayload: { attempt: rejectedPayload } }), |
| | 91 | ); |
| | 92 | const error = toPersistenceSqlError("ProviderSessionRuntimeRepository.list:query")(cause); |
| | 93 | |
| | 94 | assert.ok(error.detail !== undefined); |
| | 95 | assert.ok(!error.message.includes(rejectedPayload)); |
| | 96 | }), |
| | 97 | ); |
| | 98 | |
| 28 | it.effect("maps schema errors without copying rejected payloads into diagnostics", () => | 99 | it.effect("maps schema errors without copying rejected payloads into diagnostics", () => |
| 29 | Effect.gen(function* () { | 100 | Effect.gen(function* () { |
| 30 | const rejectedPayload = "runtime-payload-secret-sentinel"; | 101 | const rejectedPayload = "runtime-payload-secret-sentinel"; |