RustyJson.DecodeError exception (rustyjson v0.3.9)

Copy Markdown View Source

Exception raised when JSON decoding fails.

This exception is raised by RustyJson.decode!/2 when the input is not valid JSON.

Fields

  • :message - Human-readable error description
  • :data - The original input data that failed to decode
  • :position - The byte position in the input where the error occurred
  • :token - A short snippet of input around the error position

Common Causes

ErrorCause
"Unexpected character at position N"Unexpected character where a JSON value was expected
"Expected string key at position N"Object key is not a quoted string
"Unexpected end of input"JSON is truncated
"Unexpected trailing characters"Extra content after valid JSON
"Nesting depth exceeds maximum"More than 128 levels of nesting

Examples

iex> try do
...>   RustyJson.decode!("invalid")
...> rescue
...>   e in RustyJson.DecodeError ->
...>     {e.message, e.position, e.data}
...> end
{"Unexpected character at position 0", 0, "invalid"}

Handling Errors

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

case RustyJson.decode(user_input) do
  {:ok, data} -> process(data)
  {:error, reason} -> Logger.warning("Invalid JSON: #{reason}")
end

Summary

Types

t()

Decode error exception struct.

Types

t()

@type t() :: %RustyJson.DecodeError{
  __exception__: true,
  data: String.t() | nil,
  message: String.t(),
  position: non_neg_integer() | nil,
  token: String.t() | nil
}

Decode error exception struct.