Jido.BehaviorTree.Error (Jido Behavior Tree v1.0.0)

View Source

Centralized error handling for Jido BehaviorTree using Splode.

Provides two error classes:

  • :invalid - Validation and configuration errors
  • :execution - Runtime execution errors

Summary

Functions

Creates an execution error with the given message and optional details.

Creates a node-specific error with the given message, node module, and optional details.

Traverses errors, calling fun for each leaf error, and returns a nested map of results grouped by each error's path.

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

Creates a validation error with the given message and optional details.

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() :: Execution | Invalid | Splode.Error.Unknown

error_class()

@type error_class() :: :execution | :invalid | :unknown

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

execution_error(message, details \\ %{})

@spec execution_error(String.t(), map()) ::
  Jido.BehaviorTree.Error.BehaviorTreeError.t()

Creates an execution error with the given message and optional details.

node_error(message, node_module, details \\ %{})

Creates a node-specific error with the given message, node module, and optional details.

splode_error?(arg1, splode)

traverse_errors(error_or_errors, fun)

Traverses errors, calling fun for each leaf error, and returns a nested map of results grouped by each error's path.

See Splode.traverse_errors/2 for full documentation.

Example

iex> Elixir.Jido.BehaviorTree.Error.traverse_errors(error, fn error ->
...>   Exception.message(error)
...> end)
%{name: ["name is required"]}

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(message, details \\ %{})

@spec validation_error(String.t(), map()) ::
  Jido.BehaviorTree.Error.BehaviorTreeError.t()

Creates a validation error with the given message and optional details.