# CodecError

A value that does not match its method's schema: params that cannot be encoded, in which case
nothing was sent, or a result the node sent in an unexpected shape.

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

// A block count must be a non-negative integer.
const history = Eth.feeHistory(-1, "latest", []).pipe(
  Effect.catchTag("CodecError", (error) =>
    Effect.succeed(`${error.method}: bad ${error.on}`), // "eth_feeHistory: bad params"
  ),
);
```

## Properties

| Property | Type | Holds |
| --- | --- | --- |
| `_tag` | `"CodecError"` | the tag `Effect.catchTag` matches |
| `method` | `string` | the wire name of the method |
| `on` | `"params" \| "result"` | which side failed |
| `value` | `unknown` | what failed, as it was: the params as given, or the result as it was received |
| `cause` | `Error` | the schema failure, with the path of the value that did not fit: a `Schema.SchemaError` where it was made |
| `message` | `string` | `Invalid params for eth_feeHistory: …`, built from `on`, `method` and `cause` |

The message of the example above, as it prints:

```txt
Invalid params for eth_feeHistory: Expected a value between 0 and 9007199254740991
  at [0]
```

`value` stays out of the message, which it could make large. Read it to see what a node or a
wallet really sent:

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

const status = (id: string) =>
  Wallet.getCallsStatus(id).pipe(
    Effect.tapErrorTag("CodecError", (error) =>
      Effect.logWarning(`${error.method} answered`, error.value),
    ),
  );
```

## As a Schema

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

```json
{
  "_tag": "CodecError",
  "method": "wallet_getCallsStatus",
  "on": "result",
  "value": { "id": "0x01" },
  "cause": { "name": "SchemaError", "message": "Missing key\n  at [\"status\"]" }
}
```

- **`value` is written as JSON.** A `bigint` is written as hex, an `Error` as its `name` and
  its `message`, and a missing result as `null`. It is what the node or the wallet sent.
- **`cause` is written as its name and its message.** Read back, it is an `Error` named
  `SchemaError`, with the same message, and no `issue`: it is not a `Schema.SchemaError`. Its
  type is `Error`, which holds on both sides.
- **The message reads the same.** It is made of `on`, `method` and the message of the cause,
  on either side.
- **`new` checks nothing.** With fields of their types, it never throws.

## When it is raised

| `on` | Cause | Was anything sent? |
| --- | --- | --- |
| `"params"` | an argument is outside what its schema allows: a negative quantity, a `number` that is not a safe integer, hex of the wrong length | no |
| `"result"` | the node answered, and the answer does not have the shape the method promises | yes, and the node executed the call |

TypeScript catches most wrong arguments first. A `CodecError` on params is for what types
cannot express, such as the sign of a `bigint`.

## Behavior

- **Unknown fields are not an error.** A result with extra fields decodes, and the extra fields
  are dropped. Only a missing or mistyped field fails.
- **A key set to `undefined` is an absent key.** Only a provider in the page can send one,
  since JSON drops it: it is read as if it were left out. Nothing is ever encoded as
  `undefined`.
- **A wallet's answer is the cause of a `SignerError`.** What a [signer](https://ethect.dev/reference/signer/)
  cannot read of a wallet's answer fails with a `SignerError` `"Unknown"` whose `cause` is this
  error, and whose message names the method.
- **A result error is often a chain difference.** A chain whose blocks or transactions lack a
  field the schema requires fails here. Define the method again with a schema of that chain:
  see [Custom methods](https://ethect.dev/rpc/guides/custom-methods/).
- **Subscriptions and events too.** An item pushed by a subscription that does not fit fails
  the stream with a `CodecError` whose `method` is `eth_subscribe`. A payload of an
  [`Eip1193`](https://ethect.dev/reference/eip1193/) event stream that does not fit fails it with the event name
  as `method`.

## Types

| Type | Is |
| --- | --- |
| `CodecError.CodecError` | the error class |
