# `RustyJson.EncodeError`
[🔗](https://github.com/jeffhuen/rustyjson/blob/v0.3.10/lib/exceptions.ex#L1)

Exception raised when JSON encoding fails.

This exception is raised by `RustyJson.encode!/2` when the input cannot be
encoded to valid JSON.

## Fields

* `:message` - Human-readable error description

## Common Causes

| Error | Cause |
|-------|-------|
| `"Failed to decode binary"` | Binary is not valid UTF-8 |
| `"Non-finite float"` | Float is NaN or Infinity |
| `"Nesting depth exceeds maximum"` | More than 128 levels of nesting |
| `"Unsupported term type"` | Term type cannot be encoded (e.g., PID, Reference) |

## Examples

    iex> RustyJson.encode!(<<0xFF>>)
    ** (RustyJson.EncodeError) Failed to decode binary

    iex> RustyJson.encode!(:math.log(-1))
    ** (RustyJson.EncodeError) Non-finite float

## Handling Errors

Use `RustyJson.encode/2` to get `{:error, reason}` instead of raising:

    case RustyJson.encode(data) do
      {:ok, json} -> send_response(json)
      {:error, reason} -> Logger.error("Encoding failed: #{reason}")
    end

# `t`

```elixir
@type t() :: %RustyJson.EncodeError{__exception__: true, message: String.t()}
```

Encode error exception struct.

# `new`

```elixir
@spec new({:duplicate_key, term()} | {:invalid_byte, byte(), String.t()}) :: t()
```

Creates an `EncodeError` from a tagged error reason.

Matches Jason's `EncodeError.new/1` API for compatibility.

## Examples

    iex> RustyJson.EncodeError.new({:duplicate_key, "name"})
    %RustyJson.EncodeError{message: "duplicate key: name"}

    iex> RustyJson.EncodeError.new({:invalid_byte, ?\n, "hello\nworld"})
    %RustyJson.EncodeError{message: "invalid byte 0x0A in string: \"hello\\nworld\""}

---

*Consult [api-reference.md](api-reference.md) for complete listing*
