Exception: Bade::Runtime::RuntimeError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/bade/runtime.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(msg, template_backtrace = [], original: nil, print_locations_warning: false) ⇒ RuntimeError

Returns a new instance of RuntimeError.

Parameters:

  • msg (String)
  • template_backtrace (Array<Location>) (defaults to: [])
  • original (Exception, nil) (defaults to: nil)


27
28
29
30
31
32
# File 'lib/bade/runtime.rb', line 27

def initialize(msg, template_backtrace = [], original: nil, print_locations_warning: false)
  super(msg)
  @template_backtrace = template_backtrace
  @original = original
  @print_locations_warning = print_locations_warning
end

Instance Attribute Details

Returns:

  • (Boolean)


22
23
24
# File 'lib/bade/runtime.rb', line 22

def print_locations_warning
  @print_locations_warning
end

#template_backtraceArray<Location> (readonly)

Returns:



18
19
20
# File 'lib/bade/runtime.rb', line 18

def template_backtrace
  @template_backtrace
end

Class Method Details

.process_locations(locations) ⇒ Object

Parameters:

  • locations (Array<Thread::Backtrace::Location>, nil)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/bade/runtime.rb', line 75

def self.process_locations(locations)
  return [] if locations.nil?

  # map to Bade's Location
  new_locations = locations.map { |loc| Location.new(path: loc.path, lineno: loc.lineno, label: loc.label) }

  # find location to use or drop
  index = new_locations.rindex(&:template?)
  return [] if index.nil?

  # get only locations inside template
  new_locations = new_locations[0...index] || []

  # filter out not interested locations
  new_locations
    .reject { |loc| loc.path.start_with?(__dir__) }
    .reject { |loc| loc.template? && loc.label.include?('lambda_instance') }
end

.wrap_existing_error(msg, error, template_backtrace) ⇒ RuntimeError

Parameters:

Returns:



98
99
100
101
102
# File 'lib/bade/runtime.rb', line 98

def self.wrap_existing_error(msg, error, template_backtrace)
  ruby_locs = Bade::Runtime::RuntimeError.process_locations(error.backtrace_locations)
  locs = ruby_locs + template_backtrace
  Bade::Runtime::RuntimeError.new(msg, locs, original: error, print_locations_warning: !ruby_locs.empty?)
end

Instance Method Details

#__formatted_backtraceArray<String>

Returns:



64
65
66
67
68
69
70
71
72
# File 'lib/bade/runtime.rb', line 64

def __formatted_backtrace
  bt = @template_backtrace

  # delete first location if is same as second (can happen when arguments are incorrect)
  last = bt.first
  bt.delete_at(0) if last && bt.length > 1 && last == bt[1]

  bt.map { |loc| "  #{loc}" }
end

#causeObject



59
60
61
# File 'lib/bade/runtime.rb', line 59

def cause
  @original
end

#to_sObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/bade/runtime.rb', line 34

def to_s
  if @template_backtrace.empty?
    super
  else
    warning = if print_locations_warning
                <<~TEXT

                  !!! WARNING !!!, filenames and line numbers of functions can be misleading due to using Ruby
                  functions in different Bade file. Trust only functions names. Mixins are fine.

                  This will be fixed in https://github.com/epuber-io/bade/issues/32
                TEXT
              else
                ''
              end

    <<~MSG.rstrip
      #{super}
      template backtrace:
      #{__formatted_backtrace.join("\n")}
      #{warning}
    MSG
  end
end