Jido.Exec.Compensation (Jido Action v2.3.0)

View Source

Handles error compensation logic for Jido actions.

This module provides functionality to execute compensation actions when an action fails, if the action implements the on_error/4 callback and has compensation enabled in its metadata.

Summary

Functions

Checks if compensation is enabled for the given action.

Handles action errors by executing compensation if enabled.

Types

action()

@type action() :: module()

context()

@type context() :: map()

exec_result()

@type exec_result() ::
  {:ok, map()}
  | {:ok, map(), any()}
  | {:error, Exception.t()}
  | {:error, Exception.t(), any()}

params()

@type params() :: map()

run_opts()

@type run_opts() :: Jido.Exec.run_opts()

Functions

enabled?(action)

@spec enabled?(action()) :: boolean()

Checks if compensation is enabled for the given action.

Compensation is enabled if:

  1. The action's metadata includes compensation configuration with enabled: true
  2. The action exports the on_error/4 function

Parameters

  • action: The action module to check

Returns

  • true if compensation is enabled and available
  • false otherwise

handle_error(action, params, context, error_or_tuple, opts)

@spec handle_error(
  action(),
  params(),
  context(),
  Exception.t() | {Exception.t(), any()},
  run_opts()
) :: exec_result()

Handles action errors by executing compensation if enabled.

This is the main entry point for error handling with compensation. If compensation is enabled, it will execute the action's on_error/4 callback within a timeout. If compensation is disabled, it returns the original error.

Parameters

  • action: The action module that failed
  • params: The parameters that were passed to the action
  • context: The context that was passed to the action
  • error_or_tuple: The error from the failed action, either an Exception or {Exception, directive}
  • opts: Execution options including timeout

Returns

  • {:error, compensated_error} or {:error, compensated_error, directive} if compensation was attempted
  • {:error, original_error} or {:error, original_error, directive} if compensation is disabled