Auth0Client.Error exception (auth0_client v1.0.0)

Copy Markdown View Source

The single failure value returned by every Auth0Client function.

Auth0 can fail in three unrelated ways — it answers with an error status, the request never reaches it, or no management token can be obtained — and all three arrive here as one struct, so a caller needs one clause rather than three:

case Auth0Client.Management.User.get(id) do
  {:ok, user} -> user
  {:error, %Auth0Client.Error{reason: :not_found}} -> nil
  {:error, %Auth0Client.Error{} = error} -> raise error
end

It is an exception, so raise error works when a caller would rather not handle the failure at all.

Summary

Functions

Builds an error for a failure that produced no HTTP response — a transport problem, or a management token that could not be obtained.

Builds an error from an HTTP response Auth0 rejected.

The Retry-After delay in seconds, or nil when the header is absent or is not a plain number.

Types

t()

@type t() :: %Auth0Client.Error{
  __exception__: term(),
  body: term(),
  headers: %{optional(binary()) => [binary()]},
  reason: atom(),
  status: non_neg_integer() | nil
}

Functions

from_reason(reason, opts \\ [])

@spec from_reason(
  atom(),
  keyword()
) :: t()

Builds an error for a failure that produced no HTTP response — a transport problem, or a management token that could not be obtained.

from_response(status, body, headers)

@spec from_response(non_neg_integer(), term(), map()) :: t()

Builds an error from an HTTP response Auth0 rejected.

reason is derived from the status so callers can match on meaning rather than on numbers.

retry_after(error)

@spec retry_after(t()) :: non_neg_integer() | nil

The Retry-After delay in seconds, or nil when the header is absent or is not a plain number.

Auth0 sends this on a 429. Honour it rather than guessing a backoff:

{:error, %Auth0Client.Error{reason: :rate_limited} = error} ->
  Process.sleep((Auth0Client.Error.retry_after(error) || 1) * 1000)

The HTTP-date form of Retry-After is not parsed; Auth0 sends seconds.