UnifiApi.ApiError exception (UnifiApi v0.4.0)

Copy Markdown View Source

Returned for a controller response that failed but is not an authentication (401/403) or rate-limit (429) failure.

Two situations produce it:

  • A non-2xx HTTP status:status is that status and :code is nil.
  • A v1 envelope error — the legacy /api/s/... endpoints answer 200 OK carrying %{"meta" => %{"rc" => "error", "msg" => code}}. :status is then the HTTP status (normally 200) and :code is the controller's error code, e.g. "api.err.LoginRequired".

As with the other error structs the raw body is not retained; see UnifiApi.Client.scrub_body_preview/1.

Example

case UnifiApi.Network.Networks.create(client, site_id, params) do
  {:ok, network} ->
    handle(network)

  {:error, %UnifiApi.ApiError{status: 409}} ->
    :conflict

  {:error, %UnifiApi.ApiError{code: "api.err.LoginRequired"}} ->
    :session_expired
end

Summary

Types

t()

@type t() :: %UnifiApi.ApiError{
  __exception__: true,
  body_preview: binary() | nil,
  code: String.t() | nil,
  status: non_neg_integer() | nil
}