Daraja.APIError (daraja v0.1.0)

Copy Markdown View Source

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.

Summary

Functions

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

Types

status()

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

t()

@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()
}

Functions

from_body(body, opts \\ [])

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

raw_body(api_error)

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

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