Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

Errors.test.ts

88 lines3.3 KB

1import { assert, it } from "@effect/vitest";
2import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
3import * as Effect from "effect/Effect";
4import * as Schema from "effect/Schema";
5import * as SqlClient from "effect/sql/SqlClient";
6​
7import { PersistenceDecodeError, toPersistenceSqlError } from "./Errors.ts";
8​
9const decodeRuntimePayload = Schema.decodeUnknownEffect(
10 Schema.Struct({
11 runtimePayload: Schema.Struct({
12 attempt: Schema.Number,
13 }),
14 }),
15);
16​
17it.effect("names a real SQLite condition without copying query data", () =>
18 Effect.gen(function* () {
19 const sql = yield* SqlClient.SqlClient;
20 const payload = "sql-private-sentinel";
21 yield* sql`CREATE TABLE error_test (private_column TEXT PRIMARY KEY)`;
22 yield* sql`INSERT INTO error_test VALUES (${payload})`;
23 const cause = yield* Effect.flip(sql`INSERT INTO error_test VALUES (${payload})`);
24 const error = toPersistenceSqlError("OrchestrationCommandReceiptRepository.upsert:query")(
25 cause,
26 );
27​
28 assert.equal(error.detail, "SQLITE(1555) constraint failed");
29 assert.equal(error.cause, cause);
30 assert.notInclude(error.message, payload);
31 assert.notInclude(error.message, "private_column");
32 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
33);
34​
35it("reads the condition through a wrapping driver error", () => {
36 const driver = Object.assign(new Error("locked"), { errcode: 5, errstr: "database is locked" });
37 const error = toPersistenceSqlError("AuthSessionRepository.list:query")(
38 new Error("Failed to prepare statement", { cause: driver }),
39 );
40​
41 assert.equal(error.detail, "SQLITE(5) database is locked");
42});
43​
44it.each([
45 new Error("unhelpful"),
46 Object.assign(new Error("file not found"), { errno: -2, code: "ENOENT" }),
47])("omits a detail for a cause it cannot categorize (%#)", (cause) => {
48 const error = toPersistenceSqlError("AuthSessionRepository.list:query")(cause);
49​
50 assert.equal(error.detail, undefined);
51 assert.equal(error.message, "SQL error in AuthSessionRepository.list:query");
52});
53​
54it.effect("summarizes a schema cause by issue tag instead of by rejected value", () =>
55 Effect.gen(function* () {
56 const rejectedPayload = "sql-mapper-secret-sentinel";
57 const cause = yield* Effect.flip(
58 decodeRuntimePayload({ runtimePayload: { attempt: rejectedPayload } }),
59 );
60 const error = toPersistenceSqlError("ProviderSessionRuntimeRepository.list:query")(cause);
61​
62 assert.ok(error.detail !== undefined);
63 assert.ok(!error.message.includes(rejectedPayload));
64 }),
65);
66​
67it.effect("maps schema errors without copying rejected payloads into diagnostics", () =>
68 Effect.gen(function* () {
69 const rejectedPayload = "runtime-payload-secret-sentinel";
70 const cause = yield* Effect.flip(
71 decodeRuntimePayload({
72 runtimePayload: {
73 attempt: rejectedPayload,
74 },
75 }),
76 );
77 const error = PersistenceDecodeError.fromSchemaError(
78 "ProviderSessionRuntimeRepository.list:decodeRows",
79 cause,
80 );
81​
82 assert.equal(error.operation, "ProviderSessionRuntimeRepository.list:decodeRows");
83 assert.equal(error.cause, cause);
84 assert.notInclude(error.issue, rejectedPayload);
85 assert.notInclude(error.message, rejectedPayload);
86 assert.include(error.issue, "InvalidType");
87 }),
88);