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