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

Errors.ts

93 lines3.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 OrchestrationCommandInvariantError extends Schema.TaggedError<OrchestrationCommandInvariantError>()(
8 "OrchestrationCommandInvariantError",
9 {
10 commandType: Schema.String,
11 detail: Schema.String,
12 cause: Schema.optional(Schema.Defect()),
13 },
14) {
15 override get message(): string {
16 return `Orchestration command invariant failed (${this.commandType}): ${this.detail}`;
17 }
19​
20export class OrchestrationThreadSettleBlockedError extends Schema.TaggedError<OrchestrationThreadSettleBlockedError>()(
21 "OrchestrationThreadSettleBlockedError",
22 {
23 threadId: ThreadId,
24 },
25) {
26 override get message(): string {
27 return "This thread still needs attention. Resolve or interrupt it first, then try again.";
28 }
30​
31export const OrchestrationCommandRejection = Schema.Union([
32 OrchestrationCommandInvariantError,
33 OrchestrationThreadSettleBlockedError,
34]);
35export type OrchestrationCommandRejection = typeof OrchestrationCommandRejection.Type;
36export const isOrchestrationCommandRejection = Schema.is(OrchestrationCommandRejection);
37​
38export class OrchestrationCommandPreviouslyRejectedError extends Schema.TaggedError<OrchestrationCommandPreviouslyRejectedError>()(
39 "OrchestrationCommandPreviouslyRejectedError",
40 {
41 commandId: Schema.String,
42 detail: Schema.String,
43 cause: Schema.optional(Schema.Defect()),
44 },
45) {
46 override get message(): string {
47 return `Command previously rejected (${this.commandId}): ${this.detail}`;
48 }
50​
51export class OrchestrationCommandIdConflictError extends Schema.TaggedError<OrchestrationCommandIdConflictError>()(
52 "OrchestrationCommandIdConflictError",
53 {
54 commandId: Schema.String,
55 receiptAggregateKind: Schema.String,
56 receiptAggregateId: Schema.String,
57 commandAggregateKind: Schema.String,
58 commandAggregateId: Schema.String,
59 },
60) {
61 override get message(): string {
62 return `Command id '${this.commandId}' already used for ${this.receiptAggregateKind} '${this.receiptAggregateId}'; refusing to replay its receipt for ${this.commandAggregateKind} '${this.commandAggregateId}'.`;
63 }
65​
66export class OrchestrationProjectorDecodeError extends Schema.TaggedError<OrchestrationProjectorDecodeError>()(
67 "OrchestrationProjectorDecodeError",
68 {
69 eventType: Schema.String,
70 issue: Schema.String,
71 cause: Schema.optional(Schema.Defect()),
72 },
73) {
74 override get message(): string {
75 return `Projector decode failed for ${this.eventType}: ${this.issue}`;
76 }
78​
79export type OrchestrationDispatchError =
80 | ProjectionRepositoryError
81 | OrchestrationCommandRejection
82 | OrchestrationCommandIdConflictError
83 | OrchestrationCommandPreviouslyRejectedError
84 | OrchestrationProjectorDecodeError;
85​
86export function toProjectorDecodeError(eventType: string) {
87 return (error: Schema.SchemaError): OrchestrationProjectorDecodeError =>
88 new OrchestrationProjectorDecodeError({
89 eventType,
90 issue: SchemaIssue.makeFormatterDefault()(error.issue),
91 cause: error,
92 });