ReqLLM.Error (ReqLLM v1.0.0-rc.8)

View Source

Error handling system for ReqLLM using Splode.

Summary

Functions

Raises an error if the result is an error, otherwise returns the result

Creates a validation error with the given tag, reason, and context.

Types

class()

@type class() :: %{
  :__struct__ => class_module(),
  :__exception__ => true,
  :errors => [t()],
  :class => error_class(),
  :bread_crumbs => [String.t()],
  :vars => Keyword.t(),
  :stacktrace => Splode.Stacktrace.t() | nil,
  :context => map(),
  optional(atom()) => any()
}

class_module()

@type class_module() ::
  ReqLLM.Error.Unknown
  | ReqLLM.Error.Validation
  | ReqLLM.Error.API
  | ReqLLM.Error.Invalid

error_class()

@type error_class() :: :unknown | :validation | :api | :invalid

t()

@type t() :: %{
  :__struct__ => module(),
  :__exception__ => true,
  :class => error_class(),
  :bread_crumbs => [String.t()],
  :vars => Keyword.t(),
  :stacktrace => Splode.Stacktrace.t() | nil,
  :context => map(),
  optional(atom()) => any()
}

Functions

splode_error?(arg1, splode)

unwrap!(result, opts \\ nil)

Raises an error if the result is an error, otherwise returns the result

Alternatively, you can use the defsplode macro, which does this automatically.

Options

  • :error_opts - Options to pass to to_error/2 when converting the returned error
  • :unknown_error_opts - Options to pass to the unknown error if the function returns only :error. not necessary if your function always returns {:error, error}.

Examples

def function(arg) do

case do_something(arg) do
  :success -> :ok
  {:success, result} -> {:ok, result}
  {:error, error} -> {:error, error}
end

end

def function!(arg) do

YourErrors.unwrap!(function(arg))

end

validation_error(tag, reason, context \\ [])

@spec validation_error(atom(), String.t(), keyword()) ::
  ReqLLM.Error.Validation.Error.t()

Creates a validation error with the given tag, reason, and context.

Examples

iex> error = ReqLLM.Error.validation_error(:invalid_model_spec, "Bad model", model: "test")
iex> error.tag
:invalid_model_spec
iex> error.reason
"Bad model"
iex> error.context
[model: "test"]