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 OrchestrationCommandIdConflictError extends Schema.TaggedErrorClass<OrchestrationCommandIdConflictError>()(
57 "OrchestrationCommandIdConflictError",
58 {
59 commandId: Schema.String,
60 receiptAggregateKind: Schema.String,
61 receiptAggregateId: Schema.String,
62 commandAggregateKind: Schema.String,
63 commandAggregateId: Schema.String,
64 },
65) {
66 override get message(): string {
67 return `Command id '${this.commandId}' already used for ${this.receiptAggregateKind} '${this.receiptAggregateId}'; refusing to replay its receipt for ${this.commandAggregateKind} '${this.commandAggregateId}'.`;
68 }
69}
70
71export class OrchestrationProjectorDecodeError extends Schema.TaggedErrorClass<OrchestrationProjectorDecodeError>()(
72 "OrchestrationProjectorDecodeError",
73 {
74 eventType: Schema.String,
75 issue: Schema.String,
76 cause: Schema.optional(Schema.Defect()),
77 },
78) {
79 override get message(): string {
80 return `Projector decode failed for ${this.eventType}: ${this.issue}`;
81 }
82}
83
84export class OrchestrationListenerCallbackError extends Schema.TaggedErrorClass<OrchestrationListenerCallbackError>()(
85 "OrchestrationListenerCallbackError",
86 {
87 listener: Schema.Literals(["read-model", "domain-event"]),
88 detail: Schema.String,
89 cause: Schema.optional(Schema.Defect()),
90 },
91) {
92 override get message(): string {
93 return `Orchestration ${this.listener} listener failed: ${this.detail}`;
94 }
95}
96
97export type OrchestrationDispatchError =
98 | ProjectionRepositoryError
99 | OrchestrationCommandInvariantError
100 | OrchestrationCommandIdConflictError
101 | OrchestrationCommandPreviouslyRejectedError
102 | OrchestrationProjectorDecodeError
103 | OrchestrationListenerCallbackError;
104
105export type OrchestrationEngineError =
106 | OrchestrationDispatchError
107 | OrchestrationCommandJsonParseError
108 | OrchestrationCommandDecodeError;
109
110export function toOrchestrationCommandDecodeError(error: Schema.SchemaError) {
111 return new OrchestrationCommandDecodeError({
112 issue: SchemaIssue.makeFormatterDefault()(error.issue),
113 cause: error,
114 });
115}
116
117export function toProjectorDecodeError(eventType: string) {
118 return (error: Schema.SchemaError): OrchestrationProjectorDecodeError =>
119 new OrchestrationProjectorDecodeError({
120 eventType,
121 issue: SchemaIssue.makeFormatterDefault()(error.issue),
122 cause: error,
123 });
124}
125
126export function toOrchestrationJsonParseError(cause: unknown) {
127 return new OrchestrationCommandJsonParseError({
128 detail: `Failed to parse orchestration command JSON`,
129 cause,
130 });
131}
132
133export function toListenerCallbackError(listener: "read-model" | "domain-event") {
134 return (cause: unknown): OrchestrationListenerCallbackError =>
135 new OrchestrationListenerCallbackError({
136 listener,
137 detail: `Failed to invoke orchestration ${listener} listener`,
138 cause,
139 });
140}