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

Errors.ts

159 lines5.1 KB

1import { ThreadId } from "@t3tools/contracts";
2import * as SchemaIssue from "effect/SchemaIssue";
3import * as Schema from "effect/Schema";
4​
5import type { ProjectionRepositoryError } from "../persistence/Errors.ts";
6​
7export class OrchestrationCommandJsonParseError extends Schema.TaggedErrorClass<OrchestrationCommandJsonParseError>()(
8 "OrchestrationCommandJsonParseError",
9 {
10 detail: Schema.String,
11 cause: Schema.optional(Schema.Defect()),
12 },
13) {
14 override get message(): string {
15 return `Invalid orchestration command JSON: ${this.detail}`;
16 }
18​
19export class OrchestrationCommandDecodeError extends Schema.TaggedErrorClass<OrchestrationCommandDecodeError>()(
20 "OrchestrationCommandDecodeError",
21 {
22 issue: Schema.String,
23 cause: Schema.optional(Schema.Defect()),
24 },
25) {
26 override get message(): string {
27 return `Invalid orchestration command payload: ${this.issue}`;
28 }
30​
31export class OrchestrationCommandInvariantError extends Schema.TaggedErrorClass<OrchestrationCommandInvariantError>()(
32 "OrchestrationCommandInvariantError",
33 {
34 commandType: Schema.String,
35 detail: Schema.String,
36 cause: Schema.optional(Schema.Defect()),
37 },
38) {
39 override get message(): string {
40 return `Orchestration command invariant failed (${this.commandType}): ${this.detail}`;
41 }
43​
44export class OrchestrationThreadSettleBlockedError extends Schema.TaggedErrorClass<OrchestrationThreadSettleBlockedError>()(
45 "OrchestrationThreadSettleBlockedError",
46 {
47 threadId: ThreadId,
48 },
49) {
50 override get message(): string {
51 return "This thread still needs attention. Resolve or interrupt it first, then try again.";
52 }
54​
55export const OrchestrationCommandRejection = Schema.Union([
56 OrchestrationCommandInvariantError,
57 OrchestrationThreadSettleBlockedError,
58]);
59export type OrchestrationCommandRejection = typeof OrchestrationCommandRejection.Type;
60export const isOrchestrationCommandRejection = Schema.is(OrchestrationCommandRejection);
61​
62export class OrchestrationCommandPreviouslyRejectedError extends Schema.TaggedErrorClass<OrchestrationCommandPreviouslyRejectedError>()(
63 "OrchestrationCommandPreviouslyRejectedError",
64 {
65 commandId: Schema.String,
66 detail: Schema.String,
67 cause: Schema.optional(Schema.Defect()),
68 },
69) {
70 override get message(): string {
71 return `Command previously rejected (${this.commandId}): ${this.detail}`;
72 }
74​
75export class OrchestrationCommandIdConflictError extends Schema.TaggedErrorClass<OrchestrationCommandIdConflictError>()(
76 "OrchestrationCommandIdConflictError",
77 {
78 commandId: Schema.String,
79 receiptAggregateKind: Schema.String,
80 receiptAggregateId: Schema.String,
81 commandAggregateKind: Schema.String,
82 commandAggregateId: Schema.String,
83 },
84) {
85 override get message(): string {
86 return `Command id '${this.commandId}' already used for ${this.receiptAggregateKind} '${this.receiptAggregateId}'; refusing to replay its receipt for ${this.commandAggregateKind} '${this.commandAggregateId}'.`;
87 }
89​
90export class OrchestrationProjectorDecodeError extends Schema.TaggedErrorClass<OrchestrationProjectorDecodeError>()(
91 "OrchestrationProjectorDecodeError",
92 {
93 eventType: Schema.String,
94 issue: Schema.String,
95 cause: Schema.optional(Schema.Defect()),
96 },
97) {
98 override get message(): string {
99 return `Projector decode failed for ${this.eventType}: ${this.issue}`;
100 }
102​
103export class OrchestrationListenerCallbackError extends Schema.TaggedErrorClass<OrchestrationListenerCallbackError>()(
104 "OrchestrationListenerCallbackError",
105 {
106 listener: Schema.Literals(["read-model", "domain-event"]),
107 detail: Schema.String,
108 cause: Schema.optional(Schema.Defect()),
109 },
110) {
111 override get message(): string {
112 return `Orchestration ${this.listener} listener failed: ${this.detail}`;
113 }
115​
116export type OrchestrationDispatchError =
117 | ProjectionRepositoryError
118 | OrchestrationCommandRejection
119 | OrchestrationCommandIdConflictError
120 | OrchestrationCommandPreviouslyRejectedError
121 | OrchestrationProjectorDecodeError
122 | OrchestrationListenerCallbackError;
123​
124export type OrchestrationEngineError =
125 | OrchestrationDispatchError
126 | OrchestrationCommandJsonParseError
127 | OrchestrationCommandDecodeError;
128​
129export function toOrchestrationCommandDecodeError(error: Schema.SchemaError) {
130 return new OrchestrationCommandDecodeError({
131 issue: SchemaIssue.makeFormatterDefault()(error.issue),
132 cause: error,
133 });
135​
136export function toProjectorDecodeError(eventType: string) {
137 return (error: Schema.SchemaError): OrchestrationProjectorDecodeError =>
138 new OrchestrationProjectorDecodeError({
139 eventType,
140 issue: SchemaIssue.makeFormatterDefault()(error.issue),
141 cause: error,
142 });
144​
145export function toOrchestrationJsonParseError(cause: unknown) {
146 return new OrchestrationCommandJsonParseError({
147 detail: `Failed to parse orchestration command JSON`,
148 cause,
149 });
151​
152export function toListenerCallbackError(listener: "read-model" | "domain-event") {
153 return (cause: unknown): OrchestrationListenerCallbackError =>
154 new OrchestrationListenerCallbackError({
155 listener,
156 detail: `Failed to invoke orchestration ${listener} listener`,
157 cause,
158 });