RustyJson.EncodeError exception (rustyjson v0.3.9)

Copy Markdown View Source

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

ErrorCause
"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

Summary

Types

t()

Encode error exception struct.

Functions

Creates an EncodeError from a tagged error reason.

Types

t()

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

Encode error exception struct.

Functions

new(arg)

@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\""}