# `Daraja.APIError`
[🔗](https://github.com/okothkongo/daraja/blob/v0.1.1/lib/daraja/api_error.ex#L1)

Structured error for `:auth_failed`, `:http_error`, and unparsed `:request_failed`
outcomes.

Safaricom JSON error envelopes are parsed into `request_id`, `error_code`, and
`error_message`. Non-JSON bodies are summarized without including the raw
response in `inspect/1`, so callers can log `inspect(error)` safely at `:info`
or above. Use `raw_body/1` when the unparsed body is needed for debugging.

## Accepted JSON keys

Fields are read from the first matching key in each group:

  * `request_id` — `"requestId"`, `"requestid"`
  * `error_code` — `"errorCode"`, `"error"` (OAuth-style)
  * `error_message` — `"errorMessage"`, `"error_description"` (OAuth-style)

Only non-empty binary values are accepted; falsy values (`false`, `0`, `""`) are
treated as absent. A decoded JSON object must contain at least one of the keys
above or it is treated as an unrecognized error body.

# `status`

```elixir
@type status() :: pos_integer() | :unknown
```

# `t`

```elixir
@type t() :: %Daraja.APIError{
  error_code: String.t() | nil,
  error_message: String.t() | nil,
  raw_body: binary() | nil,
  request_id: String.t() | nil,
  status: status()
}
```

# `from_body`

```elixir
@spec from_body(
  binary(),
  keyword()
) :: t()
```

# `raw_body`

```elixir
@spec raw_body(t()) :: binary() | nil
```

Returns the unparsed response body when JSON decoding failed or the payload
was not a recognized error envelope.

---

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