Open History to see the last commit
1import * as SchemaIssue from "effect/SchemaIssue";
2import * as Schema from "effect/Schema";
3
4import type { ProjectionRepositoryError } from "../persistence/Errors.ts";
5
6export class OrchestrationCommandJsonParseError extends Schema.TaggedErrorClass<OrchestrationCommandJsonParseError>()(
7 "OrchestrationCommandJsonParseError",
8 {
9 detail: Schema.String,
10 cause: Schema.optional(Schema.Defect()),
11 },
12) {
13 override get message(): string {
14 return `Invalid orchestration command JSON: ${this.detail}`;
15 }
16}
17
18export class OrchestrationCommandDecodeError extends Schema.TaggedErrorClass<OrchestrationCommandDecodeError>()(
19 "OrchestrationCommandDecodeError",
20 {
21 issue: Schema.String,
22 cause: Schema.optional(Schema.Defect()),
23 },
24) {
25 override get message(): string {
26 return `Invalid orchestration command payload: ${this.issue}`;
27 }
28}
29
30export class OrchestrationCommandInvariantError extends Schema.TaggedErrorClass<OrchestrationCommandInvariantError>()(
31 "OrchestrationCommandInvariantError",
32 {
33 commandType: Schema.String,
34 detail: Schema.String,
35 cause: Schema.optional(Schema.Defect()),
36 },
37) {
38 override get message(): string {
39 return `Orchestration command invariant failed (${this.commandType}): ${this.detail}`;
40 }
41}
42
43export class OrchestrationCommandPreviouslyRejectedError extends Schema.TaggedErrorClass<OrchestrationCommandPreviouslyRejectedError>()(
44 "OrchestrationCommandPreviouslyRejectedError",
45 {
46 commandId: Schema.String,
47 detail: Schema.String,
48 cause: Schema.optional(Schema.Defect()),
49 },
50) {
51 override get message(): string {
52 return `Command previously rejected (${this.commandId}): ${this.detail}`;
53 }
54}
55
56export class OrchestrationProjectorDecodeError extends Schema.TaggedErrorClass<OrchestrationProjectorDecodeError>()(
57 "OrchestrationProjectorDecodeError",
58 {
59 eventType: Schema.String,
60 issue: Schema.String,
61 cause: Schema.optional(Schema.Defect()),
62 },
63) {
64 override get message(): string {
65 return `Projector decode failed for ${this.eventType}: ${this.issue}`;
66 }
67}
68
69export class OrchestrationListenerCallbackError extends Schema.TaggedErrorClass<OrchestrationListenerCallbackError>()(
70 "OrchestrationListenerCallbackError",
71 {
72 listener: Schema.Literals(["read-model", "domain-event"]),
73 detail: Schema.String,
74 cause: Schema.optional(Schema.Defect()),
75 },
76) {
77 override get message(): string {
78 return `Orchestration ${this.listener} listener failed: ${this.detail}`;
79 }
80}
81
82export type OrchestrationDispatchError =
83 | ProjectionRepositoryError
84 | OrchestrationCommandInvariantError
85 | OrchestrationCommandPreviouslyRejectedError
86 | OrchestrationProjectorDecodeError
87 | OrchestrationListenerCallbackError;
88
89export type OrchestrationEngineError =
90 | OrchestrationDispatchError
91 | OrchestrationCommandJsonParseError
92 | OrchestrationCommandDecodeError;
93
94export function toOrchestrationCommandDecodeError(error: Schema.SchemaError) {
95 return new OrchestrationCommandDecodeError({
96 issue: SchemaIssue.makeFormatterDefault()(error.issue),
97 cause: error,
98 });
99}
100
101export function toProjectorDecodeError(eventType: string) {
102 return (error: Schema.SchemaError): OrchestrationProjectorDecodeError =>
103 new OrchestrationProjectorDecodeError({
104 eventType,
105 issue: SchemaIssue.makeFormatterDefault()(error.issue),
106 cause: error,
107 });
108}
109
110export function toOrchestrationJsonParseError(cause: unknown) {
111 return new OrchestrationCommandJsonParseError({
112 detail: `Failed to parse orchestration command JSON`,
113 cause,
114 });
115}
116
117export function toListenerCallbackError(listener: "read-model" | "domain-event") {
118 return (cause: unknown): OrchestrationListenerCallbackError =>
119 new OrchestrationListenerCallbackError({
120 listener,
121 detail: `Failed to invoke orchestration ${listener} listener`,
122 cause,
123 });
124}