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

Errors.ts

172 lines5.7 KB

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 }
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 }
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 }
73const isPersistenceSqlError = Schema.is(PersistenceSqlError);
74const isPersistenceDecodeError = Schema.is(PersistenceDecodeError);
75​
76/**
77 * Read a SQLite condition through SQL error wrappers.
78 * Use node:sqlite's fixed description, 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 value = "cause" in value ? value.cause : undefined;
92 }
93 return undefined;
95​
96/**
97 * A rejected payload must never reach diagnostics, so a schema failure
98 * contributes only its issue tags, and a driver failure only its normalized
99 * condition. Anything the mapper cannot categorize leaves the detail unset.
100 */
101function describeSqlCause(cause: unknown): string | undefined {
102 if (Schema.isSchemaError(cause)) return summarizeSchemaIssue(cause.issue);
103 return sqliteCondition(cause);
105​
106// Kept for orchestration/projection call sites, which are being revamped separately.
107export function toPersistenceSqlError(operation: string) {
108 return (cause: unknown): PersistenceSqlError => {
109 const detail = describeSqlCause(cause);
110 return new PersistenceSqlError({
111 operation,
112 ...(detail === undefined ? {} : { detail }),
113 cause,
114 });
115 };
117​
118// Kept for orchestration/projection call sites, which are being revamped separately.
119export function toPersistenceDecodeError(operation: string) {
120 return (cause: Schema.SchemaError): PersistenceDecodeError =>
121 PersistenceDecodeError.fromSchemaError(operation, cause);
123​
124export const isPersistenceError = (u: unknown) =>
125 isPersistenceSqlError(u) || isPersistenceDecodeError(u);
126​
127// ===============================
128// Provider Session Repository Errors
129// ===============================
130​
131export class ProviderSessionRepositoryValidationError extends Schema.TaggedError<ProviderSessionRepositoryValidationError>()(
132 "ProviderSessionRepositoryValidationError",
133 {
134 operation: Schema.String,
135 issue: Schema.String,
136 cause: Schema.optional(Schema.Defect()),
137 },
138) {
139 override get message(): string {
140 return `Provider session repository validation failed in ${this.operation}: ${this.issue}`;
141 }
143​
144export class ProviderSessionRepositoryPersistenceError extends Schema.TaggedError<ProviderSessionRepositoryPersistenceError>()(
145 "ProviderSessionRepositoryPersistenceError",
146 {
147 operation: Schema.String,
148 detail: Schema.String,
149 cause: Schema.optional(Schema.Defect()),
150 },
151) {
152 override get message(): string {
153 return `Provider session repository persistence error in ${this.operation}: ${this.detail}`;
154 }
156​
157export type OrchestrationEventStoreError = PersistenceSqlError | PersistenceDecodeError;
158​
159export type ProviderSessionRepositoryError =
160 | ProviderSessionRepositoryValidationError
161 | ProviderSessionRepositoryPersistenceError;
162​
163export type OrchestrationCommandReceiptRepositoryError =
164 | PersistenceSqlError
165 | PersistenceDecodeError;
166​
167export type ProviderSessionRuntimeRepositoryError = PersistenceSqlError | PersistenceDecodeError;
168export type AuthPairingLinkRepositoryError = PersistenceSqlError | PersistenceDecodeError;
169export type AuthSessionRepositoryError = PersistenceSqlError | PersistenceDecodeError;
170export type PullRequestFilesViewedRepositoryError = PersistenceSqlError | PersistenceDecodeError;
171​
172export type ProjectionRepositoryError = PersistenceSqlError | PersistenceDecodeError;