1/**
2 * Client-side protocol failures reported by unstable RPC transports.
3 *
4 * `RpcClientError` is the error type generated clients use when a call fails
5 * before a remote handler can return its declared typed error. Its `reason`
6 * covers built-in transport failures from HTTP, sockets, and workers, plus
7 * `RpcClientDefect` values for malformed or incompatible protocol data.
8 *
9 * @stability unstable
10 * @since 4.0.0
11 */
12import { HttpClientErrorSchema } from "../http/HttpClientError.ts"
13import * as Schema from "../Schema.ts"
14import { SocketErrorReason } from "../socket/Socket.ts"
15import { WorkerErrorReason } from "../workers/WorkerError.ts"
16
17const TypeId = "~effect/rpc/RpcClientError"
18
19/**
20 * Represents a client-side RPC defect, such as a protocol violation or
21 * decoding failure, with a message and original cause.
22 *
23 * @stability unstable
24 * @category errors
25 * @since 4.0.0
26 */
27export class RpcClientDefect extends Schema.Error<RpcClientDefect>("effect/rpc/RpcClientError/RpcClientDefect")({
28 _tag: Schema.tag("RpcClientDefect"),
29 message: Schema.String,
30 cause: Schema.Defect()
31}) {}
32
33/**
34 * Error wrapper for RPC client failures, including worker, socket, HTTP client,
35 * and client protocol defect failures.
36 *
37 * @stability unstable
38 * @category errors
39 * @since 4.0.0
40 */
41export class RpcClientError extends Schema.Error<RpcClientError>(TypeId)({
42 _tag: Schema.tag("RpcClientError"),
43 reason: Schema.Union([
44 WorkerErrorReason,
45 SocketErrorReason,
46 HttpClientErrorSchema,
47 RpcClientDefect
48 ])
49}) {
50 /**
51 * Marks this value as an RPC client error for runtime guards.
52 *
53 * @stability unstable
54 * @since 4.0.0
55 */
56 readonly [TypeId] = TypeId
57
58 override get message(): string {
59 return `${this.reason._tag}: ${this.reason.message}`
60 }
61}