@devries-cameron20 / t3code Mirror · read-only / commits / 9c98cd60e8
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 24, 22:45 UTC

Remove persistence error constructor wrappers (#3398)

Co-authored-by: codex <codex@users.noreply.github.com>

Julius Marminge committed Jun 20, 2026 at 11:26 UTC 9c98cd60e866fb576e7029cd1152f75aca06452c
5 files changed +104 -34
apps/server/src/persistence/AuthPairingLinks.ts
+4 -4
@@ -10,8 +10,8 @@ import { AuthEnvironmentScopes } from "@t3tools/contracts";
10
11 import {
12 type AuthPairingLinkRepositoryError,
13 - toPersistenceDecodeError,
14 - toPersistenceSqlError,
13 + PersistenceDecodeError,
14 + PersistenceSqlError,
15 } from "./Errors.ts";
16
17 export const AuthPairingLinkRecord = Schema.Struct({
@@ -90,8 +90,8 @@ export class AuthPairingLinkRepository extends Context.Service<
90 function toPersistenceSqlOrDecodeError(sqlOperation: string, decodeOperation: string) {
91 return (cause: unknown): AuthPairingLinkRepositoryError =>
92 Schema.isSchemaError(cause)
93 - ? toPersistenceDecodeError(decodeOperation)(cause)
94 - : toPersistenceSqlError(sqlOperation)(cause);
93 + ? PersistenceDecodeError.fromSchemaError(decodeOperation, cause)
94 + : new PersistenceSqlError({ operation: sqlOperation, cause });
95 }
96
97 export const make = Effect.gen(function* () {
apps/server/src/persistence/AuthSessions.ts
+4 -4
@@ -15,8 +15,8 @@ import {
15
16 import {
17 type AuthSessionRepositoryError,
18 - toPersistenceDecodeError,
19 - toPersistenceSqlError,
18 + PersistenceDecodeError,
19 + PersistenceSqlError,
20 } from "./Errors.ts";
21
22 export const AuthSessionClientMetadataRecord = Schema.Struct({
@@ -146,8 +146,8 @@ function toAuthSessionRecord(row: typeof AuthSessionDbRow.Type): AuthSessionReco
146 function toPersistenceSqlOrDecodeError(sqlOperation: string, decodeOperation: string) {
147 return (cause: unknown): AuthSessionRepositoryError =>
148 Schema.isSchemaError(cause)
149 - ? toPersistenceDecodeError(decodeOperation)(cause)
150 - : toPersistenceSqlError(sqlOperation)(cause);
149 + ? PersistenceDecodeError.fromSchemaError(decodeOperation, cause)
150 + : new PersistenceSqlError({ operation: sqlOperation, cause });
151 }
152
153 export const make = Effect.gen(function* () {
apps/server/src/persistence/Errors.test.ts new
+49
@@ -0,0 +1,49 @@
1 +import { assert, it } from "@effect/vitest";
2 +import * as Effect from "effect/Effect";
3 +import * as Schema from "effect/Schema";
4 +
5 +import { PersistenceDecodeError, PersistenceSqlError } from "./Errors.ts";
6 +
7 +const decodeRuntimePayload = Schema.decodeUnknownEffect(
8 + Schema.Struct({
9 + runtimePayload: Schema.Struct({
10 + attempt: Schema.Number,
11 + }),
12 + }),
13 +);
14 +
15 +it("keeps SQL operation context without a tautological detail", () => {
16 + const cause = new Error("database unavailable");
17 + const error = new PersistenceSqlError({
18 + operation: "AuthSessionRepository.list:query",
19 + cause,
20 + });
21 +
22 + assert.equal(error.operation, "AuthSessionRepository.list:query");
23 + assert.equal(error.detail, undefined);
24 + assert.equal(error.cause, cause);
25 + assert.equal(error.message, "SQL error in AuthSessionRepository.list:query");
26 +});
27 +
28 +it.effect("maps schema errors without copying rejected payloads into diagnostics", () =>
29 + Effect.gen(function* () {
30 + const rejectedPayload = "runtime-payload-secret-sentinel";
31 + const cause = yield* Effect.flip(
32 + decodeRuntimePayload({
33 + runtimePayload: {
34 + attempt: rejectedPayload,
35 + },
36 + }),
37 + );
38 + const error = PersistenceDecodeError.fromSchemaError(
39 + "ProviderSessionRuntimeRepository.list:decodeRows",
40 + cause,
41 + );
42 +
43 + assert.equal(error.operation, "ProviderSessionRuntimeRepository.list:decodeRows");
44 + assert.equal(error.cause, cause);
45 + assert.notInclude(error.issue, rejectedPayload);
46 + assert.notInclude(error.message, rejectedPayload);
47 + assert.include(error.issue, "InvalidType");
48 + }),
49 +);
apps/server/src/persistence/Errors.ts
+30 -17
@@ -1,6 +1,20 @@
1 import * as Schema from "effect/Schema";
2 import * as SchemaIssue from "effect/SchemaIssue";
3
4 +function summarizeSchemaIssue(issue: SchemaIssue.Issue): string {
5 + switch (issue._tag) {
6 + case "Filter":
7 + case "Encoding":
8 + case "Pointer":
9 + return `${issue._tag}(${summarizeSchemaIssue(issue.issue)})`;
10 + case "Composite":
11 + case "AnyOf":
12 + return `${issue._tag}(${issue.issues.map(summarizeSchemaIssue).join(",")})`;
13 + default:
14 + return issue._tag;
15 + }
16 +}
17 +
18 // ===============================
19 // Core Persistence Errors
20 // ===============================
@@ -9,12 +23,14 @@ export class PersistenceSqlError extends Schema.TaggedErrorClass<PersistenceSqlE
23 "PersistenceSqlError",
24 {
25 operation: Schema.String,
12 - detail: Schema.String,
26 + detail: Schema.optional(Schema.String),
27 cause: Schema.optional(Schema.Defect()),
28 },
29 ) {
30 override get message(): string {
17 - return `SQL error in ${this.operation}: ${this.detail}`;
31 + return this.detail === undefined
32 + ? `SQL error in ${this.operation}`
33 + : `SQL error in ${this.operation}: ${this.detail}`;
34 }
35 }
36
@@ -26,6 +42,14 @@ export class PersistenceDecodeError extends Schema.TaggedErrorClass<PersistenceD
42 cause: Schema.optional(Schema.Defect()),
43 },
44 ) {
45 + static fromSchemaError(operation: string, cause: Schema.SchemaError): PersistenceDecodeError {
46 + return new PersistenceDecodeError({
47 + operation,
48 + issue: summarizeSchemaIssue(cause.issue),
49 + cause,
50 + });
51 + }
52 +
53 override get message(): string {
54 return `Decode error in ${this.operation}: ${this.issue}`;
55 }
@@ -33,6 +57,7 @@ export class PersistenceDecodeError extends Schema.TaggedErrorClass<PersistenceD
57 const isPersistenceSqlError = Schema.is(PersistenceSqlError);
58 const isPersistenceDecodeError = Schema.is(PersistenceDecodeError);
59
60 +// Kept for orchestration/projection call sites, which are being revamped separately.
61 export function toPersistenceSqlError(operation: string) {
62 return (cause: unknown): PersistenceSqlError =>
63 new PersistenceSqlError({
@@ -42,22 +67,10 @@ export function toPersistenceSqlError(operation: string) {
67 });
68 }
69
70 +// Kept for orchestration/projection call sites, which are being revamped separately.
71 export function toPersistenceDecodeError(operation: string) {
46 - return (error: Schema.SchemaError): PersistenceDecodeError =>
47 - new PersistenceDecodeError({
48 - operation,
49 - issue: SchemaIssue.makeFormatterDefault()(error.issue),
50 - cause: error,
51 - });
52 -}
53 -
54 -export function toPersistenceDecodeCauseError(operation: string) {
55 - return (cause: unknown): PersistenceDecodeError =>
56 - new PersistenceDecodeError({
57 - operation,
58 - issue: `Failed to execute ${operation}`,
59 - cause,
60 - });
72 + return (cause: Schema.SchemaError): PersistenceDecodeError =>
73 + PersistenceDecodeError.fromSchemaError(operation, cause);
74 }
75
76 export const isPersistenceError = (u: unknown) =>
apps/server/src/persistence/ProviderSessionRuntime.ts
+17 -9
@@ -16,9 +16,9 @@ import {
16 } from "@t3tools/contracts";
17
18 import {
19 + PersistenceDecodeError,
20 + PersistenceSqlError,
21 type ProviderSessionRuntimeRepositoryError,
20 - toPersistenceDecodeError,
21 - toPersistenceSqlError,
22 } from "./Errors.ts";
23
24 /**
@@ -117,8 +117,8 @@ const DeleteRuntimeRequestSchema = GetRuntimeRequestSchema;
117 function toPersistenceSqlOrDecodeError(sqlOperation: string, decodeOperation: string) {
118 return (cause: unknown): ProviderSessionRuntimeRepositoryError =>
119 Schema.isSchemaError(cause)
120 - ? toPersistenceDecodeError(decodeOperation)(cause)
121 - : toPersistenceSqlError(sqlOperation)(cause);
120 + ? PersistenceDecodeError.fromSchemaError(decodeOperation, cause)
121 + : new PersistenceSqlError({ operation: sqlOperation, cause });
122 }
123
124 export const make = Effect.gen(function* () {
@@ -235,9 +235,10 @@ export const make = Effect.gen(function* () {
235 onNone: () => Effect.succeed(Option.none()),
236 onSome: (row) =>
237 decodeRuntime(row).pipe(
238 - Effect.mapError(
239 - toPersistenceDecodeError(
238 + Effect.mapError((cause) =>
239 + PersistenceDecodeError.fromSchemaError(
240 "ProviderSessionRuntimeRepository.getByThreadId:rowToRuntime",
241 + cause,
242 ),
243 ),
244 Effect.map((runtime) => Option.some(runtime)),
@@ -259,8 +260,11 @@ export const make = Effect.gen(function* () {
260 rows,
261 (row) =>
262 decodeRuntime(row).pipe(
262 - Effect.mapError(
263 - toPersistenceDecodeError("ProviderSessionRuntimeRepository.list:rowToRuntime"),
263 + Effect.mapError((cause) =>
264 + PersistenceDecodeError.fromSchemaError(
265 + "ProviderSessionRuntimeRepository.list:rowToRuntime",
266 + cause,
267 + ),
268 ),
269 ),
270 { concurrency: "unbounded" },
@@ -273,7 +277,11 @@ export const make = Effect.gen(function* () {
277 ) =>
278 deleteRuntimeByThreadId(input).pipe(
279 Effect.mapError(
276 - toPersistenceSqlError("ProviderSessionRuntimeRepository.deleteByThreadId:query"),
280 + (cause) =>
281 + new PersistenceSqlError({
282 + operation: "ProviderSessionRuntimeRepository.deleteByThreadId:query",
283 + cause,
284 + }),
285 ),
286 );
287