ReqFly.Error exception (req_fly v1.0.0)

View Source

Error struct and exception handling for ReqFly operations.

This module defines the error structure used throughout ReqFly to represent failed HTTP requests and API errors from the Fly.io Machines API.

Fields

  • :status - HTTP status code (e.g., 404, 500)
  • :code - Error code from the API response
  • :reason - Human-readable error message
  • :request_id - Fly request ID from the "fly-request-id" header
  • :body - Raw response body
  • :method - HTTP method used (e.g., "GET", "POST")
  • :url - Request URL

Examples

iex> error = %ReqFly.Error{status: 404, reason: "Machine not found"}
iex> Exception.message(error)
"[404] Machine not found"

Summary

Functions

Creates an error from an exception or error tuple.

Parses a Req.Response into a ReqFly.Error struct.

Returns a human-readable error message.

Types

t()

@type t() :: %ReqFly.Error{
  __exception__: true,
  body: any(),
  code: String.t() | nil,
  method: String.t() | nil,
  reason: String.t() | nil,
  request_id: String.t() | nil,
  status: non_neg_integer() | nil,
  url: String.t() | nil
}

Functions

from_exception(exception)

@spec from_exception(Exception.t() | {:error, term()}) :: t()

Creates an error from an exception or error tuple.

Examples

iex> ReqFly.Error.from_exception(%Mint.TransportError{reason: :timeout})
%ReqFly.Error{reason: "timeout"}

from_response(response)

@spec from_response(Req.Response.t()) :: t()

Parses a Req.Response into a ReqFly.Error struct.

Extracts error information from JSON responses with "error", "message", or "code" keys. Also extracts the "fly-request-id" header if present.

Examples

iex> response = %Req.Response{
...>   status: 404,
...>   body: %{"error" => "not_found", "message" => "Machine not found"},
...>   headers: %{"fly-request-id" => ["abc123"]}
...> }
iex> ReqFly.Error.from_response(response)
%ReqFly.Error{
  status: 404,
  code: "not_found",
  reason: "Machine not found",
  request_id: "abc123",
  body: %{"error" => "not_found", "message" => "Machine not found"}
}

from_response(response, opts)

@spec from_response(
  Req.Response.t(),
  keyword()
) :: t()

message(error)

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

Returns a human-readable error message.

Examples

iex> error = %ReqFly.Error{status: 404, reason: "Not found"}
iex> Exception.message(error)
"[404] Not found"

iex> error = %ReqFly.Error{reason: "Connection failed"}
iex> Exception.message(error)
"Connection failed"