Module: Trailblazer::Developer::Wtf

Defined in:
lib/trailblazer/developer/wtf.rb,
lib/trailblazer/developer/wtf/renderer.rb

Defined Under Namespace

Modules: Exception, Renderer

Class Method Summary collapse

Class Method Details

.invoke(activity, ctx, flow_options, present_options: {}, **circuit_options) ⇒ Object

Run activity with tracing enabled and inject a mutable Stack instance. This allows to display the trace even when an exception happened



17
18
19
20
21
22
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
60
61
62
63
64
65
66
67
# File 'lib/trailblazer/developer/wtf.rb', line 17

def invoke(activity, (ctx, flow_options), present_options: {}, **circuit_options)
  flow_options ||= {}
  local_present_options_block = ->(*) { {} }

  stack = Trace::Stack.new # unfortunately, we need this mutable object before things break.
  raise_exception = false

  begin
    complete_stack, signal, (ctx, flow_options) = Trace.invoke(
      activity,
      [ctx, flow_options.merge(stack: stack)],
      **circuit_options
    )
  rescue
    raise_exception = $! # TODO: will this show the very same stacktrace?

    exception_source  = Exception.find_exception_source(stack, $!)
    complete_stack    = stack

    local_present_options_block = ->(trace_nodes:, **) {
      exception_source_node = trace_nodes.reverse.find do |trace_node|
        trace_node.snapshot_after == exception_source || trace_node.snapshot_before == exception_source
      end

      {
        # we can hand in options per node, identified by their captured_input part.
        node_options: {
          exception_source_node => {data: {exception_source: true}}, # goes to {Debugger::Node.build}
        },
        style: {
          exception_source_node => [:red, :bold]
        },
      }
    }
  end

  # always render the trace.
  output, returned_args = Trace::Present.(
    complete_stack,
    renderer:   Wtf::Renderer,
    color_map:  Wtf::Renderer::DEFAULT_COLOR_MAP.merge(flow_options[:color_map] || {}),
    activity:   activity,
    **present_options,
    &local_present_options_block
  )

  puts output

  raise raise_exception if raise_exception
  return signal, [ctx, flow_options], circuit_options, output, returned_args
end