Gleanex.Error exception (Gleanex v0.1.0)

Copy Markdown View Source

Every way a Glean call can fail, in one struct.

Operations return {:ok, result} or {:error, %Gleanex.Error{}} and never both. Match on :reason to tell failures apart:

  • :config - bad or missing settings, raised or returned before any request is sent. Missing token, unresolvable domain, wrong token scope.
  • :transport - the request never got a response: timeout, refused connection, TLS failure. exception holds the underlying error.
  • :rate_limited - HTTP 429. retry_after holds the header value in seconds when Glean sent one.
  • :problem_detail - the response carried an RFC 7807 body, parsed into problem.
  • :http - any other unsuccessful status, with the decoded body.

Examples

case Gleanex.Client.Search.search(body, config: config) do
  {:ok, results} -> results
  {:error, %Gleanex.Error{reason: :rate_limited, retry_after: seconds}} -> back_off(seconds)
  {:error, %Gleanex.Error{reason: :problem_detail, problem: problem}} -> Logger.error(problem.detail)
  {:error, error} -> raise error
end

Summary

Functions

A configuration failure. Raised by Gleanex.new/1, returned by scope checks.

Wrap a transport level exception.

Wrap an unsuccessful response.

Types

reason()

@type reason() :: :config | :transport | :rate_limited | :problem_detail | :http

t()

@type t() :: %Gleanex.Error{
  __exception__: term(),
  body: term(),
  exception: Exception.t() | nil,
  message: String.t(),
  operation: {module(), atom()} | nil,
  problem: Gleanex.ProblemDetail.t() | nil,
  reason: reason(),
  retry_after: integer() | nil,
  status: integer() | nil
}

Functions

config(message)

@spec config(String.t()) :: t()

A configuration failure. Raised by Gleanex.new/1, returned by scope checks.

from_exception(exception, operation \\ nil)

@spec from_exception(Exception.t(), {module(), atom()} | nil) :: t()

Wrap a transport level exception.

from_response(response, operation \\ nil)

@spec from_response(Req.Response.t(), {module(), atom()} | nil) :: t()

Wrap an unsuccessful response.

operation is the {module, function} pair the generated code reports, used only to make the message readable.