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

Errors.ts

138 lines4.6 KB

1import * as Schema from "effect/Schema";
2import * as SchemaIssue from "effect/SchemaIssue";
3​
4function 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 }
17​
18// ===============================
19// Core Persistence Errors
20// ===============================
21​
22export const PersistenceErrorCorrelation = Schema.Union([
23 Schema.Struct({ sessionId: Schema.String }),
24 Schema.Struct({ currentSessionId: Schema.String }),
25 Schema.Struct({ pairingLinkId: Schema.String }),
26 Schema.Struct({ threadId: Schema.String }),
27]);
28export type PersistenceErrorCorrelation = typeof PersistenceErrorCorrelation.Type;
29​
30export class PersistenceSqlError extends Schema.TaggedErrorClass<PersistenceSqlError>()(
31 "PersistenceSqlError",
32 {
33 operation: Schema.String,
34 detail: Schema.optional(Schema.String),
35 correlation: Schema.optional(PersistenceErrorCorrelation),
36 cause: Schema.optional(Schema.Defect()),
37 },
38) {
39 override get message(): string {
40 return this.detail === undefined
41 ? `SQL error in ${this.operation}`
42 : `SQL error in ${this.operation}: ${this.detail}`;
43 }
45​
46export class PersistenceDecodeError extends Schema.TaggedErrorClass<PersistenceDecodeError>()(
47 "PersistenceDecodeError",
48 {
49 operation: Schema.String,
50 issue: Schema.String,
51 correlation: Schema.optional(PersistenceErrorCorrelation),
52 cause: Schema.optional(Schema.Defect()),
53 },
54) {
55 static fromSchemaError(
56 operation: string,
57 cause: Schema.SchemaError,
58 correlation?: PersistenceErrorCorrelation,
59 ): PersistenceDecodeError {
60 return new PersistenceDecodeError({
61 operation,
62 issue: summarizeSchemaIssue(cause.issue),
63 ...(correlation === undefined ? {} : { correlation }),
64 cause,
65 });
66 }
67​
68 override get message(): string {
69 return `Decode error in ${this.operation}: ${this.issue}`;
70 }
72const isPersistenceSqlError = Schema.is(PersistenceSqlError);
73const isPersistenceDecodeError = Schema.is(PersistenceDecodeError);
74​
75// Kept for orchestration/projection call sites, which are being revamped separately.
76export function toPersistenceSqlError(operation: string) {
77 return (cause: unknown): PersistenceSqlError =>
78 new PersistenceSqlError({
79 operation,
80 detail: `Failed to execute ${operation}`,
81 cause,
82 });
84​
85// Kept for orchestration/projection call sites, which are being revamped separately.
86export function toPersistenceDecodeError(operation: string) {
87 return (cause: Schema.SchemaError): PersistenceDecodeError =>
88 PersistenceDecodeError.fromSchemaError(operation, cause);
90​
91export const isPersistenceError = (u: unknown) =>
92 isPersistenceSqlError(u) || isPersistenceDecodeError(u);
93​
94// ===============================
95// Provider Session Repository Errors
96// ===============================
97​
98export class ProviderSessionRepositoryValidationError extends Schema.TaggedErrorClass<ProviderSessionRepositoryValidationError>()(
99 "ProviderSessionRepositoryValidationError",
100 {
101 operation: Schema.String,
102 issue: Schema.String,
103 cause: Schema.optional(Schema.Defect()),
104 },
105) {
106 override get message(): string {
107 return `Provider session repository validation failed in ${this.operation}: ${this.issue}`;
108 }
110​
111export class ProviderSessionRepositoryPersistenceError extends Schema.TaggedErrorClass<ProviderSessionRepositoryPersistenceError>()(
112 "ProviderSessionRepositoryPersistenceError",
113 {
114 operation: Schema.String,
115 detail: Schema.String,
116 cause: Schema.optional(Schema.Defect()),
117 },
118) {
119 override get message(): string {
120 return `Provider session repository persistence error in ${this.operation}: ${this.detail}`;
121 }
123​
124export type OrchestrationEventStoreError = PersistenceSqlError | PersistenceDecodeError;
125​
126export type ProviderSessionRepositoryError =
127 | ProviderSessionRepositoryValidationError
128 | ProviderSessionRepositoryPersistenceError;
129​
130export type OrchestrationCommandReceiptRepositoryError =
131 | PersistenceSqlError
132 | PersistenceDecodeError;
133​
134export type ProviderSessionRuntimeRepositoryError = PersistenceSqlError | PersistenceDecodeError;
135export type AuthPairingLinkRepositoryError = PersistenceSqlError | PersistenceDecodeError;
136export type AuthSessionRepositoryError = PersistenceSqlError | PersistenceDecodeError;
137​
138export type ProjectionRepositoryError = PersistenceSqlError | PersistenceDecodeError;