Skip to content

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.

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
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.
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
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"),
}),
);
  • 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.
  • 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.
  • 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.

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

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,
}),
);
Type Is
TransportError.TransportError the error class
TransportError.Reason the union of the reasons above

A custom transport builds one with new:

import { TransportError } from "@ethect/core";
const lost = new TransportError.TransportError({
reason: { _tag: "ConnectionLost" },
message: "The tunnel closed before the node answered",
});