| 1 | /** |
| 2 | * Typed error model for worker APIs. |
| 3 | * |
| 4 | * This module defines the `WorkerError` wrapper, the reason variants for spawn, |
| 5 | * send, receive, and unknown worker failures, a schema union for those reasons, |
| 6 | * and a guard for recognizing worker errors at runtime. |
| 7 | * |
| 8 | * @stability unstable |
| 9 | * @since 4.0.0 |
| 10 | */ |
| 11 | import { hasProperty } from "../Predicate.ts" |
| 12 | import * as Schema from "../Schema.ts" |
| 13 | |
| 14 | const TypeId = "~effect/workers/WorkerError" as const |
| 15 | |
| 16 | /** |
| 17 | * Type-level identifier used to brand `WorkerError` values. |
| 18 | * |
| 19 | * @stability unstable |
| 20 | * @category type IDs |
| 21 | * @since 4.0.0 |
| 22 | */ |
| 23 | export type TypeId = typeof TypeId |
| 24 | |
| 25 | /** |
| 26 | * Returns `true` when a value is a `WorkerError`. |
| 27 | * |
| 28 | * @stability unstable |
| 29 | * @category guards |
| 30 | * @since 4.0.0 |
| 31 | */ |
| 32 | export const isWorkerError = (u: unknown): u is WorkerError => hasProperty(u, TypeId) |
| 33 | |
| 34 | /** |
| 35 | * Worker error reason for failures while spawning or setting up a worker. |
| 36 | * |
| 37 | * @stability unstable |
| 38 | * @category errors |
| 39 | * @since 4.0.0 |
| 40 | */ |
| 41 | export class WorkerSpawnError extends Schema.Error<WorkerSpawnError>( |
| 42 | "effect/workers/WorkerError/WorkerSpawnError" |
| 43 | )({ |
| 44 | _tag: Schema.tag("WorkerSpawnError"), |
| 45 | message: Schema.String, |
| 46 | cause: Schema.optional(Schema.Defect()) |
| 47 | }) {} |
| 48 | |
| 49 | /** |
| 50 | * Worker error reason for failures while sending a message to a worker. |
| 51 | * |
| 52 | * @stability unstable |
| 53 | * @category errors |
| 54 | * @since 4.0.0 |
| 55 | */ |
| 56 | export class WorkerSendError extends Schema.Error<WorkerSendError>( |
| 57 | "effect/workers/WorkerError/WorkerSendError" |
| 58 | )({ |
| 59 | _tag: Schema.tag("WorkerSendError"), |
| 60 | message: Schema.String, |
| 61 | cause: Schema.optional(Schema.Defect()) |
| 62 | }) {} |
| 63 | |
| 64 | /** |
| 65 | * Worker error reason for failures while receiving or handling a message from a |
| 66 | * worker. |
| 67 | * |
| 68 | * @stability unstable |
| 69 | * @category errors |
| 70 | * @since 4.0.0 |
| 71 | */ |
| 72 | export class WorkerReceiveError extends Schema.Error<WorkerReceiveError>( |
| 73 | "effect/workers/WorkerError/WorkerReceiveError" |
| 74 | )({ |
| 75 | _tag: Schema.tag("WorkerReceiveError"), |
| 76 | message: Schema.String, |
| 77 | cause: Schema.optional(Schema.Defect()) |
| 78 | }) {} |
| 79 | |
| 80 | /** |
| 81 | * Worker error reason for an unclassified worker failure. |
| 82 | * |
| 83 | * @stability unstable |
| 84 | * @category errors |
| 85 | * @since 4.0.0 |
| 86 | */ |
| 87 | export class WorkerUnknownError extends Schema.Error<WorkerUnknownError>( |
| 88 | "effect/workers/WorkerError/WorkerUnknownError" |
| 89 | )({ |
| 90 | _tag: Schema.tag("WorkerUnknownError"), |
| 91 | message: Schema.String, |
| 92 | cause: Schema.optional(Schema.Defect()) |
| 93 | }) {} |
| 94 | |
| 95 | /** |
| 96 | * Union of the specific failure reasons that can be wrapped by a `WorkerError`. |
| 97 | * |
| 98 | * @stability unstable |
| 99 | * @category errors |
| 100 | * @since 4.0.0 |
| 101 | */ |
| 102 | export type WorkerErrorReason = |
| 103 | | WorkerSpawnError |
| 104 | | WorkerSendError |
| 105 | | WorkerReceiveError |
| 106 | | WorkerUnknownError |
| 107 | |
| 108 | /** |
| 109 | * Schema for decoding and encoding all supported worker error reason variants. |
| 110 | * |
| 111 | * @stability unstable |
| 112 | * @category schemas |
| 113 | * @since 4.0.0 |
| 114 | */ |
| 115 | export const WorkerErrorReason: Schema.Union<[ |
| 116 | typeof WorkerSpawnError, |
| 117 | typeof WorkerSendError, |
| 118 | typeof WorkerReceiveError, |
| 119 | typeof WorkerUnknownError |
| 120 | ]> = Schema.Union([ |
| 121 | WorkerSpawnError, |
| 122 | WorkerSendError, |
| 123 | WorkerReceiveError, |
| 124 | WorkerUnknownError |
| 125 | ]) |
| 126 | |
| 127 | /** |
| 128 | * Error raised by worker APIs, wrapping a specific `WorkerErrorReason` and |
| 129 | * exposing its message and cause. |
| 130 | * |
| 131 | * @stability unstable |
| 132 | * @category errors |
| 133 | * @since 4.0.0 |
| 134 | */ |
| 135 | export class WorkerError extends Schema.Error<WorkerError>(TypeId)({ |
| 136 | _tag: Schema.tag("WorkerError"), |
| 137 | reason: WorkerErrorReason |
| 138 | }) { |
| 139 | // @effect-diagnostics-next-line overriddenSchemaConstructor:off |
| 140 | constructor(props: { |
| 141 | readonly reason: WorkerErrorReason |
| 142 | }) { |
| 143 | super({ |
| 144 | ...props, |
| 145 | cause: props.reason.cause |
| 146 | } as any) |
| 147 | } |
| 148 | /** |
| 149 | * Marks this value as a worker error for runtime guards. |
| 150 | * |
| 151 | * @stability unstable |
| 152 | * @since 4.0.0 |
| 153 | */ |
| 154 | readonly [TypeId]: TypeId = TypeId |
| 155 | |
| 156 | override get message(): string { |
| 157 | return this.reason.message |
| 158 | } |
| 159 | } |