The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Data from "effect/Data"; |
| 2 | import * as Predicate from "effect/Predicate"; |
| 3 | |
| 4 | // ---------------------------------------------------------------- definitions |
| 5 | |
| 6 | /** Two different event classes share a tag. */ |
| 7 | export class DuplicateEventTag extends Data.TaggedError("DuplicateEventTag")<{ |
| 8 | readonly tag: string; |
| 9 | }> { |
| 10 | override get message() { |
| 11 | return `Two different events are tagged '${this.tag}'`; |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | /** Two aggregates, views or streams share a name. */ |
| 16 | export class DuplicateName extends Data.TaggedError("DuplicateName")<{ |
| 17 | readonly entity: string; |
| 18 | }> { |
| 19 | override get message() { |
| 20 | return `Two entities are named '${this.entity}'`; |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | /** The Domain lists the same aggregate, policy or projection twice. */ |
| 25 | export class DuplicateDefinition extends Data.TaggedError("DuplicateDefinition")<{ |
| 26 | readonly kind: "aggregate" | "policy" | "projection"; |
| 27 | readonly entity: string; |
| 28 | }> { |
| 29 | override get message() { |
| 30 | return `Duplicate ${this.kind} '${this.entity}'`; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | /** An `initial` state its state schema cannot encode. */ |
| 35 | export class InvalidInitial extends Data.TaggedError("InvalidInitial")<{ |
| 36 | readonly owner: string; |
| 37 | readonly cause: unknown; |
| 38 | }> { |
| 39 | override get message() { |
| 40 | const detail = Predicate.hasProperty(this.cause, "message") |
| 41 | ? String(this.cause.message) |
| 42 | : String(this.cause); |
| 43 | return `${this.owner}.initial does not match its state schema: ${detail}`; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /** A view declares state but no `initial`. */ |
| 48 | export class MissingInitial extends Data.TaggedError("MissingInitial")<{ |
| 49 | readonly view: string; |
| 50 | }> { |
| 51 | override get message() { |
| 52 | return `${this.view} declares state without initial`; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** A view, policy or projection consumes something the Domain does not list. */ |
| 57 | export class UnknownSource extends Data.TaggedError("UnknownSource")<{ |
| 58 | readonly consumer: string; |
| 59 | readonly source: string; |
| 60 | }> { |
| 61 | override get message() { |
| 62 | return `${this.consumer} consumes ${this.source}, which is not in the Domain`; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** A view consumes its own output through other views. */ |
| 67 | export class ConsumesItself extends Data.TaggedError("ConsumesItself")<{ |
| 68 | readonly view: string; |
| 69 | }> { |
| 70 | override get message() { |
| 71 | return `${this.view} consumes itself through other views`; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** A consumer keyed differently from its source names no `keyOf` for an event. */ |
| 76 | export class MissingKeyOf extends Data.TaggedError("MissingKeyOf")<{ |
| 77 | readonly consumer: string; |
| 78 | readonly event: string; |
| 79 | readonly source: string; |
| 80 | /** The aggregate the consumer is keyed by, or undefined for a key schema. */ |
| 81 | readonly keyedBy?: string; |
| 82 | }> { |
| 83 | override get message() { |
| 84 | return `${this.consumer} consumes '${this.event}' from ${this.source}, which is not keyed by ${this.keyedBy ?? "its key schema"}: add keyOf.${this.event}`; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /** A routed handler names a route that `keyOf` does not produce. */ |
| 89 | export class UnmatchedRoute extends Data.TaggedError("UnmatchedRoute")<{ |
| 90 | readonly view: string; |
| 91 | readonly handler: string; |
| 92 | readonly source: string; |
| 93 | readonly route: string; |
| 94 | }> { |
| 95 | override get message() { |
| 96 | return `${this.view}.${this.handler}.${this.source}.${this.route} has no matching route in keyOf.${this.source}`; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** A consumer subscribes to an event none of its sources emits. */ |
| 101 | export class NoEmitter extends Data.TaggedError("NoEmitter")<{ |
| 102 | readonly consumer: string; |
| 103 | readonly sources: ReadonlyArray<string>; |
| 104 | readonly event: string; |
| 105 | }> { |
| 106 | override get message() { |
| 107 | return `${this.consumer}: none of ${this.sources.join(", ")} emits '${this.event}'`; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** A projection writes a stream the Domain does not list. */ |
| 112 | export class UnknownStream extends Data.TaggedError("UnknownStream")<{ |
| 113 | readonly writer: string; |
| 114 | readonly stream: string; |
| 115 | }> { |
| 116 | override get message() { |
| 117 | return `${this.writer} writes ${this.stream}, which is not in the Domain`; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /** Two projections write the same stream. */ |
| 122 | export class TwoWriters extends Data.TaggedError("TwoWriters")<{ |
| 123 | readonly stream: string; |
| 124 | readonly first: string; |
| 125 | readonly second: string; |
| 126 | }> { |
| 127 | override get message() { |
| 128 | return `${this.stream} has two writers, ${this.first} and ${this.second}: a stream key has exactly one`; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /** A projection writes a stream keyed differently from itself. */ |
| 133 | export class WriterKeyMismatch extends Data.TaggedError("WriterKeyMismatch")<{ |
| 134 | readonly writer: string; |
| 135 | readonly key: string; |
| 136 | readonly stream: string; |
| 137 | readonly streamKey: string; |
| 138 | }> { |
| 139 | override get message() { |
| 140 | return `${this.writer} is keyed by ${this.key}, so it cannot write ${this.stream}, keyed by ${this.streamKey}`; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // ---------------------------------------------------------------- runtime |
| 145 | |
| 146 | /** An aggregate receives a command it does not list. */ |
| 147 | export class UnhandledCommand extends Data.TaggedError("UnhandledCommand")<{ |
| 148 | readonly aggregate: string; |
| 149 | readonly command: string; |
| 150 | }> { |
| 151 | override get message() { |
| 152 | return `${this.aggregate} does not handle '${this.command}'`; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /** An Api exposes a command no aggregate in the Domain handles. */ |
| 157 | export class UnroutedCommand extends Data.TaggedError("UnroutedCommand")<{ |
| 158 | readonly command: string; |
| 159 | }> { |
| 160 | override get message() { |
| 161 | return `${this.command} is served, but no hosted aggregate handles it`; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /** `decide` returns an event or rejection its command does not declare. */ |
| 166 | export class UndeclaredDecision extends Data.TaggedError("UndeclaredDecision")<{ |
| 167 | readonly aggregate: string; |
| 168 | readonly command: string; |
| 169 | readonly returned: string; |
| 170 | readonly list: "events" | "rejects"; |
| 171 | }> { |
| 172 | override get message() { |
| 173 | return `${this.aggregate}.decide.${this.command} returned '${this.returned}', which ${this.command} does not declare in ${this.list}`; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /** An aggregate commits an event it does not declare. */ |
| 178 | export class UndeclaredEvent extends Data.TaggedError("UndeclaredEvent")<{ |
| 179 | readonly aggregate: string; |
| 180 | readonly event: string; |
| 181 | }> { |
| 182 | override get message() { |
| 183 | return `${this.aggregate} does not declare event '${this.event}'`; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /** A view's `emit` returns an event its `events` do not list. */ |
| 188 | export class UndeclaredEmit extends Data.TaggedError("UndeclaredEmit")<{ |
| 189 | readonly view: string; |
| 190 | readonly event: string; |
| 191 | }> { |
| 192 | override get message() { |
| 193 | return `${this.view} emitted '${this.event}', which it does not declare in events`; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /** A stored event's tag matches no event in the Domain. */ |
| 198 | export class UnknownEvent extends Data.TaggedError("UnknownEvent")<{ |
| 199 | readonly event: string; |
| 200 | }> { |
| 201 | override get message() { |
| 202 | return `Unknown event '${this.event}'`; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /** A request carries a context service nothing registered. */ |
| 207 | export class UnknownRequestContext extends Data.TaggedError("UnknownRequestContext")<{ |
| 208 | readonly key: string; |
| 209 | }> { |
| 210 | override get message() { |
| 211 | return `Unknown request context '${this.key}'`; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | /** An Api exposes an aggregate that declares no `read` contract. */ |
| 216 | export class Unreadable extends Data.TaggedError("Unreadable")<{ |
| 217 | readonly aggregate: string; |
| 218 | }> { |
| 219 | override get message() { |
| 220 | return `${this.aggregate} has no \`read\` contract, so an Api cannot expose it`; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /** An external system declares an operation named after a static of its class. */ |
| 225 | export class ReservedOperation extends Data.TaggedError("ReservedOperation")<{ |
| 226 | readonly system: string; |
| 227 | readonly operation: string; |
| 228 | }> { |
| 229 | override get message() { |
| 230 | return `ExternalSystem '${this.system}' cannot declare an operation named '${this.operation}'`; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | /** The Domain has sagas, but the platform runs no workflows. */ |
| 235 | export class MissingWorkflowEngine extends Data.TaggedError("MissingWorkflowEngine")<{ |
| 236 | readonly sagas: ReadonlyArray<string>; |
| 237 | }> { |
| 238 | override get message() { |
| 239 | return `${this.sagas.join(", ")} need a platform that runs workflows`; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | /** Something other than the stream's writer, handling its input, appends to it. */ |
| 244 | export class ForeignAppend extends Data.TaggedError("ForeignAppend")<{ |
| 245 | readonly stream: string; |
| 246 | }> { |
| 247 | override get message() { |
| 248 | return `Only the projection that lists ${this.stream} in \`writes\` may append to it, with the input it is handling`; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /** An Api names something its model does not hold. */ |
| 253 | export class NotInModel extends Data.TaggedError("NotInModel")<{ |
| 254 | readonly entity: string; |
| 255 | readonly model: string; |
| 256 | }> { |
| 257 | override get message() { |
| 258 | return `${this.entity} is not part of the ${this.model} model`; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /** An Api call makes a tool name longer than model providers accept. */ |
| 263 | export class ToolNameTooLong extends Data.TaggedError("ToolNameTooLong")<{ |
| 264 | readonly tool: string; |
| 265 | readonly max: number; |
| 266 | }> { |
| 267 | override get message() { |
| 268 | return `The tool name ${this.tool} is longer than ${this.max} characters`; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /** Two calls of an Api make the same tool name. */ |
| 273 | export class DuplicateTool extends Data.TaggedError("DuplicateTool")<{ |
| 274 | readonly api: string; |
| 275 | readonly tool: string; |
| 276 | }> { |
| 277 | override get message() { |
| 278 | return `Two calls of ${this.api} make the tool ${this.tool}`; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | /** A payload field would be a flag the CLI reserves. */ |
| 283 | export class ReservedFlag extends Data.TaggedError("ReservedFlag")<{ |
| 284 | readonly call: string; |
| 285 | readonly field: string; |
| 286 | readonly flag: string; |
| 287 | }> { |
| 288 | override get message() { |
| 289 | return `${this.call}: field \`${this.field}\` would be the flag --${this.flag}, which the CLI reserves`; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | /** Two payload fields would be the same CLI flag. */ |
| 294 | export class DuplicateFlag extends Data.TaggedError("DuplicateFlag")<{ |
| 295 | readonly call: string; |
| 296 | readonly field: string; |
| 297 | readonly flag: string; |
| 298 | readonly taken: string; |
| 299 | }> { |
| 300 | override get message() { |
| 301 | return `${this.call}: field \`${this.field}\` would be the flag --${this.flag}, as is field \`${this.taken}\``; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | /** Two calls of an Api would be the same CLI command. */ |
| 306 | export class DuplicateCommand extends Data.TaggedError("DuplicateCommand")<{ |
| 307 | readonly api: string; |
| 308 | readonly first: string; |
| 309 | readonly second: string; |
| 310 | readonly command: string; |
| 311 | }> { |
| 312 | override get message() { |
| 313 | return `${this.api}: \`${this.first}\` and \`${this.second}\` are both the command ${this.command}`; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | /** An Api's HttpApi group is served from an HttpApi that does not hold it. */ |
| 318 | export class MissingGroup extends Data.TaggedError("MissingGroup")<{ |
| 319 | readonly httpApi: string; |
| 320 | readonly group: string; |
| 321 | }> { |
| 322 | override get message() { |
| 323 | return `${this.httpApi} does not hold the ${this.group} group: add it first`; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | /** A consumer receives a delivery for a newer generation than the code it runs. */ |
| 328 | export class NewerGeneration extends Data.TaggedError("NewerGeneration")<{ |
| 329 | readonly consumer: string; |
| 330 | readonly sent: number; |
| 331 | readonly runs: number; |
| 332 | }> { |
| 333 | override get message() { |
| 334 | return `${this.consumer} received generation ${this.sent} but runs ${this.runs}: this instance runs older code`; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | /** Two different models with the same name are hosted on one platform, so they would share storage. */ |
| 339 | export class ModelHostedTwice extends Data.TaggedError("ModelHostedTwice")<{ |
| 340 | readonly model: string; |
| 341 | }> { |
| 342 | override get message() { |
| 343 | return `Model ${this.model} is hosted twice on one platform: give each model its own name`; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | /** A view set to fill lazily isn't keyed by its one source, so a key can't find its own history. */ |
| 348 | export class LazyViewNeedsOneSource extends Data.TaggedError("LazyViewNeedsOneSource")<{ |
| 349 | readonly view: string; |
| 350 | }> { |
| 351 | override get message() { |
| 352 | return `${this.view} can't be lazy: a lazy view must read one aggregate and be keyed by it, so each key reads its own instance's history. Leave it out of \`lazy\`, and the deploy fills it.`; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | /** Every reason a {@link DefinitionError} has. */ |
| 357 | export type Reason = |
| 358 | | DuplicateEventTag |
| 359 | | DuplicateName |
| 360 | | DuplicateDefinition |
| 361 | | InvalidInitial |
| 362 | | MissingInitial |
| 363 | | UnknownSource |
| 364 | | ConsumesItself |
| 365 | | MissingKeyOf |
| 366 | | UnmatchedRoute |
| 367 | | NoEmitter |
| 368 | | UnknownStream |
| 369 | | TwoWriters |
| 370 | | WriterKeyMismatch |
| 371 | | UnhandledCommand |
| 372 | | UnroutedCommand |
| 373 | | UndeclaredDecision |
| 374 | | UndeclaredEvent |
| 375 | | UndeclaredEmit |
| 376 | | UnknownEvent |
| 377 | | UnknownRequestContext |
| 378 | | Unreadable |
| 379 | | ReservedOperation |
| 380 | | MissingWorkflowEngine |
| 381 | | ForeignAppend |
| 382 | | NotInModel |
| 383 | | ToolNameTooLong |
| 384 | | DuplicateTool |
| 385 | | ReservedFlag |
| 386 | | DuplicateFlag |
| 387 | | DuplicateCommand |
| 388 | | MissingGroup |
| 389 | | NewerGeneration |
| 390 | | ModelHostedTwice |
| 391 | | LazyViewNeedsOneSource; |
| 392 | |
| 393 | /** |
| 394 | * A domain definition error, found while the kernel is built or when a |
| 395 | * handler returns something its declaration does not allow. Its `reason` |
| 396 | * says which problem it is: match it with `Effect.catchReason`. |
| 397 | */ |
| 398 | export class DefinitionError extends Data.TaggedError("DefinitionError")<{ |
| 399 | readonly reason: Reason; |
| 400 | }> { |
| 401 | override get message() { |
| 402 | return this.reason.message; |
| 403 | } |
| 404 | } |