# TransportError

A call that produced no JSON-RPC answer: the node could not be reached, the connection
dropped, or what came back was not JSON-RPC.

```ts
import { Effect } from "effect";
import { Eth } from "@ethect/core/rpc";

const head = Eth.blockNumber().pipe(
  Effect.map((number): bigint | undefined => number),
  Effect.catchReason("TransportError", "Unreachable", () =>
    Effect.succeed(undefined),
  ),
);
// Effect<bigint | undefined, …>: undefined while the node is down
```

## Properties

| Property | Type | Holds |
| --- | --- | --- |
| `_tag` | `"TransportError"` | the tag `Effect.catchTag` matches |
| `reason` | `TransportError.Reason` | why no answer came back |
| `message` | `string` | a sentence for a log |
| `cause` | `unknown` (optional) | the underlying failure: the HTTP client's error, the socket's error, a parse error. For a transport whose URL is a secret, a plain error with the tag and the message of that failure, [without the URL](https://ethect.dev/reference/transport/http/#url). It is [never written](#as-a-schema). |

## Reasons

| Reason | Fields | Means | Raised by |
| --- | --- | --- | --- |
| `Unreachable` | | No connection could be made, or the request could not be sent. | every transport |
| `HttpStatus` | `status`, `body` | The server answered with a non-2xx status and a body that is not JSON-RPC. | HTTP |
| `ConnectionLost` | | The socket closed while the call was in flight. | WebSocket, IPC |
| `MalformedResponse` | `body` | The answer is not valid JSON-RPC, or is missing. | every transport |
| `Unserializable` | | The request cannot be written as JSON, so nothing was sent. | HTTP, WebSocket, IPC |

```ts
import { Effect } from "effect";
import { Eth } from "@ethect/core/rpc";

const head = Eth.blockNumber().pipe(
  Effect.map((number) => `block ${number}`),
  Effect.catchReasons("TransportError", {
    // reason.status is a number, reason.body the text of the response
    HttpStatus: (reason) => Effect.succeed(`HTTP ${reason.status}`),
    Unreachable: () => Effect.succeed("node down"),
    ConnectionLost: () => Effect.succeed("connection dropped"),
  }),
);
```

## As a Schema

`TransportError.TransportError` is its own Schema. Declared in the failure of a query or of a
command, it is written as its tag, its `reason` and its `message`, and read back as a
`TransportError.TransportError`.

| The error | Written as |
| --- | --- |
| an error page | `{ "_tag": "TransportError", "reason": { "_tag": "HttpStatus", "status": 502, "body": "Bad gateway" }, "message": "<redacted> answered with HTTP status 502" }` |
| a node that is down | `{ "_tag": "TransportError", "reason": { "_tag": "Unreachable" }, "message": "The HTTP request to <redacted> failed" }` |

- **`cause` is never written, and is absent once read.** It is the HTTP client's or the
  socket's own error. Its message holds the URL, and its own cause can hold an internal
  address, such as `connect ECONNREFUSED 10.0.0.7:8545`.
- **The `body` of a `MalformedResponse` is written as JSON.** It is what the server sent.
- **The message holds no secret when the URL is `Redacted`.** A plain string URL is in the
  message, as it is in a log. A server whose failures cross a wire reads its URL with
  `Config.Redacted`: see [the `url` of `Transport.http`](https://ethect.dev/reference/transport/http/#url).
- **`new` checks nothing.** With fields of their types, it never throws.

[`Transport.Error`](https://ethect.dev/reference/transport/#types) is the Schema of an `RpcError` or a
`TransportError`.

## Behavior

- **Was the call executed?** `Unreachable` and `Unserializable` mean the node never received
  it. After `ConnectionLost`, `HttpStatus` or `MalformedResponse` it may have: do not repeat a
  call that sends a transaction without checking.
- **An error status with a JSON-RPC body is not `HttpStatus`.** The body is read, and a
  JSON-RPC error in it is an [`RpcError`](https://ethect.dev/reference/errors/rpc-error/).
- **EIP-1193.** A provider that rejects with something other than a JSON-RPC error object is
  a `MalformedResponse`, with the rejection as `cause`.
- **Sockets.** While a socket transport cannot reach its node, calls fail at once with
  `Unreachable` and the message `The node cannot be reached`. A URL the socket cannot use
  fails the same way. See [Connection lifecycle](https://ethect.dev/concepts/connection-lifecycle/).
- **One bad message fails one call.** Over a socket or in an HTTP batch, an answer that is
  JSON but not JSON-RPC is a `MalformedResponse` for the call it names only.
- **EIP-1193 events.** A provider whose events cannot be listened to fails its event streams
  and its subscriptions with `Unreachable`. See [`Eip1193`](https://ethect.dev/reference/eip1193/).

### Retrying

Retry on the reasons that are safe for the call at hand.

```ts
import { Effect, Schedule } from "effect";
import { Eth } from "@ethect/core/rpc";

// A read: repeating it cannot do harm.
const head = Eth.blockNumber().pipe(
  Effect.retry({
    while: (error) => error._tag === "TransportError",
    schedule: Schedule.exponential("200 millis"),
    times: 3,
  }),
);
```

## Types

| Type | Is |
| --- | --- |
| `TransportError.TransportError` | the error class |
| `TransportError.Reason` | the union of the reasons above |

A [custom transport](https://ethect.dev/guides/custom-transports/) builds one with `new`:

```ts
import { TransportError } from "@ethect/core";

const lost = new TransportError.TransportError({
  reason: { _tag: "ConnectionLost" },
  message: "The tunnel closed before the node answered",
});
```
