Class: Chronos::Core::ExceptionCauseCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/chronos/core/exception_cause_collector.rb

Overview

Collects a bounded exception cause chain with cycle protection.

Examples:

collector.call(exception)

Constant Summary collapse

DEFAULT_MAX_DEPTH =
10

Instance Method Summary collapse

Constructor Details

#initialize(max_depth = DEFAULT_MAX_DEPTH) ⇒ ExceptionCauseCollector

Returns a new instance of ExceptionCauseCollector.



17
18
19
# File 'lib/chronos/core/exception_cause_collector.rb', line 17

def initialize(max_depth = DEFAULT_MAX_DEPTH)
  @max_depth = max_depth
end

Instance Method Details

#call(exception) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/chronos/core/exception_cause_collector.rb', line 21

def call(exception)
  causes = []
  seen = {}
  current = exception

  @max_depth.times do
    break unless current.respond_to?(:cause)
    current = safe_cause(current)
    break unless current
    break if seen[current.object_id]

    seen[current.object_id] = true
    causes << {
      "class" => safe_class_name(current),
      "message" => safe_message(current)
    }
  end

  causes
end