Module: Axn::Internal::ExceptionContext

Defined in:
lib/axn/internal/exception_context.rb

Overview

Builds context data for exception reporting to error tracking services (e.g., Honeybadger, Sentry).

Class Method Summary collapse

Class Method Details

.build(action:, retry_context: nil) ⇒ Hash

Build enhanced context for global on_exception handler. This method is called internally by Axn’s exception handling system.

Parameters:

Returns:

  • (Hash)

    Enhanced context with structure: {

    inputs: { ... },              # User's action inputs (filtered for sensitive data, always formatted)
    outputs: { ... },             # Action outputs (filtered for sensitive data, always formatted)
    ...extra_keys...,             # Additional context from set_execution_context / hook (formatted)
    retry_command: "...",         # Optional: copy-pasteable retry command (if _include_retry_command_in_exceptions is true)
    current_attributes: { ... },  # Optional: Current.attributes if defined and present
    async: { ... }                # Optional: async retry context if applicable
    

    }



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/axn/internal/exception_context.rb', line 23

def build(action:, retry_context: nil)
  # Get structured execution context (inputs, outputs, and extra keys at top level)
  exec_ctx = action.execution_context
  raw_inputs = exec_ctx[:inputs]

  # Start building the context with formatted execution context
  context = {
    inputs: format_hash_values(raw_inputs),
    outputs: format_hash_values(exec_ctx[:outputs] || {}),
  }

  # Add any extra keys from execution context (from set_execution_context / hook)
  extra_keys = exec_ctx.except(:inputs, :outputs)
  context.merge!(format_hash_values(extra_keys)) if extra_keys.any?

  # Add async information if available
  context[:async] = retry_context.to_h if retry_context

  # Auto-generate retry command if experimental flag is enabled
  if Axn.config._include_retry_command_in_exceptions
    # Use raw_inputs (not formatted) for retry command generation
    # so we can generate proper Model.find() calls from AR objects
    context[:retry_command] = retry_command(
      action:,
      context: raw_inputs,
    )
  end

  # Auto-include Current.attributes if defined, responds to attributes, and has non-nil values
  if defined?(Current) && Current.respond_to?(:attributes)
    current_attrs = Current.attributes
    # Only include if the hash has any non-nil values
    context[:current_attributes] = format_hash_values(current_attrs) if current_attrs.present? && current_attrs.any? { |_k, v| !v.nil? }
  end

  context
end