Open History to see the last commit
1import * as Predicate from "effect/Predicate";
2import * as Schema from "effect/Schema";
3import * as SchemaIssue from "effect/SchemaIssue";
4
5function summarizeSchemaIssue(issue: SchemaIssue.Issue): string {
6 switch (issue._tag) {
7 case "Filter":
8 case "Encoding":
9 case "Pointer":
10 return `${issue._tag}(${summarizeSchemaIssue(issue.issue)})`;
11 case "Composite":
12 case "AnyOf":
13 return `${issue._tag}(${issue.issues.map(summarizeSchemaIssue).join(",")})`;
14 default:
15 return issue._tag;
16 }
17}
18
19// ===============================
20// Core Persistence Errors
21// ===============================
22
23export const PersistenceErrorCorrelation = Schema.Union([
24 Schema.Struct({ sessionId: Schema.String }),
25 Schema.Struct({ currentSessionId: Schema.String }),
26 Schema.Struct({ pairingLinkId: Schema.String }),
27 Schema.Struct({ threadId: Schema.String }),
28]);
29export type PersistenceErrorCorrelation = typeof PersistenceErrorCorrelation.Type;
30
31export class PersistenceSqlError extends Schema.TaggedError<PersistenceSqlError>()(
32 "PersistenceSqlError",
33 {
34 operation: Schema.String,
35 detail: Schema.optional(Schema.String),
36 correlation: Schema.optional(PersistenceErrorCorrelation),
37 cause: Schema.optional(Schema.Defect()),
38 },
39) {
40 override get message(): string {
41 return this.detail === undefined
42 ? `SQL error in ${this.operation}`
43 : `SQL error in ${this.operation}: ${this.detail}`;
44 }
45}
46
47export class PersistenceDecodeError extends Schema.TaggedError<PersistenceDecodeError>()(
48 "PersistenceDecodeError",
49 {
50 operation: Schema.String,
51 issue: Schema.String,
52 correlation: Schema.optional(PersistenceErrorCorrelation),
53 cause: Schema.optional(Schema.Defect()),
54 },
55) {
56 static fromSchemaError(
57 operation: string,
58 cause: Schema.SchemaError,
59 correlation?: PersistenceErrorCorrelation,
60 ): PersistenceDecodeError {
61 return new PersistenceDecodeError({
62 operation,
63 issue: summarizeSchemaIssue(cause.issue),
64 ...(correlation === undefined ? {} : { correlation }),
65 cause,
66 });
67 }
68
69 override get message(): string {
70 return `Decode error in ${this.operation}: ${this.issue}`;
71 }
72}
73const isPersistenceSqlError = Schema.is(PersistenceSqlError);
74const isPersistenceDecodeError = Schema.is(PersistenceDecodeError);
75
76/**
77 * Read a SQLite condition through SQL error wrappers.
78 * Use Node's fixed description or Bun's numeric code, never the driver message.
79 */
80function sqliteCondition(cause: unknown): string | undefined {
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})`;
99 }
100 value = "cause" in value ? value.cause : undefined;
101 }
102 return undefined;
103}
104
105/**
106 * A rejected payload must never reach diagnostics, so a schema failure
107 * contributes only its issue tags, and a driver failure only its normalized
108 * condition. Anything the mapper cannot categorize leaves the detail unset.
109 */
110function describeSqlCause(cause: unknown): string | undefined {
111 if (Schema.isSchemaError(cause)) return summarizeSchemaIssue(cause.issue);
112 return sqliteCondition(cause);
113}
114
115// Kept for orchestration/projection call sites, which are being revamped separately.
116export function toPersistenceSqlError(operation: string) {
117 return (cause: unknown): PersistenceSqlError => {
118 const detail = describeSqlCause(cause);
119 return new PersistenceSqlError({
120 operation,
121 ...(detail === undefined ? {} : { detail }),
122 cause,
123 });
124 };
125}
126
127// Kept for orchestration/projection call sites, which are being revamped separately.
128export function toPersistenceDecodeError(operation: string) {
129 return (cause: Schema.SchemaError): PersistenceDecodeError =>
130 PersistenceDecodeError.fromSchemaError(operation, cause);
131}
132
133export const isPersistenceError = (u: unknown) =>
134 isPersistenceSqlError(u) || isPersistenceDecodeError(u);
135
136// ===============================
137// Provider Session Repository Errors
138// ===============================
139
140export class ProviderSessionRepositoryValidationError extends Schema.TaggedError<ProviderSessionRepositoryValidationError>()(
141 "ProviderSessionRepositoryValidationError",
142 {
143 operation: Schema.String,
144 issue: Schema.String,
145 cause: Schema.optional(Schema.Defect()),
146 },
147) {
148 override get message(): string {
149 return `Provider session repository validation failed in ${this.operation}: ${this.issue}`;
150 }
151}
152
153export class ProviderSessionRepositoryPersistenceError extends Schema.TaggedError<ProviderSessionRepositoryPersistenceError>()(
154 "ProviderSessionRepositoryPersistenceError",
155 {
156 operation: Schema.String,
157 detail: Schema.String,
158 cause: Schema.optional(Schema.Defect()),
159 },
160) {
161 override get message(): string {
162 return `Provider session repository persistence error in ${this.operation}: ${this.detail}`;
163 }
164}
165
166export type OrchestrationEventStoreError = PersistenceSqlError | PersistenceDecodeError;
167
168export type ProviderSessionRepositoryError =
169 | ProviderSessionRepositoryValidationError
170 | ProviderSessionRepositoryPersistenceError;
171
172export type OrchestrationCommandReceiptRepositoryError =
173 | PersistenceSqlError
174 | PersistenceDecodeError;
175
176export type ProviderSessionRuntimeRepositoryError = PersistenceSqlError | PersistenceDecodeError;
177export type AuthPairingLinkRepositoryError = PersistenceSqlError | PersistenceDecodeError;
178export type AuthSessionRepositoryError = PersistenceSqlError | PersistenceDecodeError;
179
180export type ProjectionRepositoryError = PersistenceSqlError | PersistenceDecodeError;