Module: Trailblazer::Developer::Wtf

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

Defined Under Namespace

Modules: String

Class Method Summary collapse

Class Method Details

.exception_renderer(stack:, level:, input:, name:, closest_task:) ⇒ Object



46
47
48
49
# File 'lib/trailblazer/developer/wtf.rb', line 46

def exception_renderer(stack:, level:, input:, name:, closest_task:)
  return [ level, %{#{fmt(fmt(name, :red), :bold)}} ] if input.task == closest_task
  [ level, %{#{name}} ]
end

.fmt(line, style) ⇒ Object



63
64
65
# File 'lib/trailblazer/developer/wtf.rb', line 63

def fmt(line, style)
  String.send(style, line)
end

.handle(stack, exception, closest_task, activity, *args) ⇒ Object

TODO: make this injectable



52
53
54
55
56
57
58
59
# File 'lib/trailblazer/developer/wtf.rb', line 52

def handle(stack, exception, closest_task, activity, *args)
  puts "[Trailblazer] Exception tracing"
  puts "#{fmt(exception.inspect, :bold)}"
  puts "    #{exception.backtrace[0]}"
  puts "    #{exception.backtrace[1]}"
  puts
  puts Trailblazer::Activity::Trace::Present.(stack, closest_task: closest_task, renderer: method(:exception_renderer))
end

.invoke(activity, ctx, flow_options, *args) ⇒ 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
# File 'lib/trailblazer/developer/wtf.rb', line 17

def invoke(activity, (ctx, flow_options), *args)
  flow_options ||= {} # Ruby sucks.

  # this instance gets mutated with every step. unfortunately, there is
  # no other way in Ruby to keep the trace even when an exception was thrown.
  stack = Trailblazer::Activity::Trace::Stack.new

  begin
    _returned_stack, *returned = Trailblazer::Activity::Trace.invoke( activity,
      [
        ctx,
        flow_options.merge(stack: stack)
      ],
      *args
    )
  rescue
    # DISCUSS: we shouldn't use internal knowledge of the Stack/Level API here.
    closest = stack.to_a
    while closest.is_a?(Trailblazer::Activity::Trace::Level) && closest = closest.last do # FIXME: deep-dive via Stack API.
    end

    # pp closest.task # this was the last executed task

    handle(stack, $!, closest.task, activity, [ctx, flow_options])
  end

  returned # FIXME: test me
end