Class: RenderMePretty::Erb::BaseHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/render_me_pretty/erb/base_handler.rb

Direct Known Subclasses

MainErrorHandler, SyntaxErrorHandler

Instance Method Summary collapse

Constructor Details

#initialize(exception, path, layout_path = nil) ⇒ BaseHandler



3
4
5
6
7
# File 'lib/render_me_pretty/erb/base_handler.rb', line 3

def initialize(exception, path, layout_path = nil)
  @exception = exception
  @path = path
  @layout_path = layout_path
end

Instance Method Details

#backtrace_lines ⇒ Object

Method produces a filtered original stack trace that can be appended to the pretty backtrace output.

It parses the original backtrace that looks something like this:

(erb):380:in `get_binding'
/Users/tung/.rbenv/versions/2.5.0/lib/ruby/2.5.0/erb.rb:885:in `eval'
/Users/tung/.rbenv/versions/2.5.0/lib/ruby/2.5.0/erb.rb:885:in `result'
/Users/tung/src/tongueroo/lono/vendor/render_me_pretty/lib/render_me_pretty/erb.rb:67:in `render'
/Users/tung/src/tongueroo/lono/vendor/render_me_pretty/lib/render_me_pretty.rb:11:in `result'
/Users/tung/src/tongueroo/lono/lib/lono/template/template.rb:32:in `build'
/Users/tung/src/tongueroo/lono/lib/lono/template/dsl.rb:82:in `block in build_templates'
/Users/tung/src/tongueroo/lono/lib/lono/template/dsl.rb:81:in `each'


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/render_me_pretty/erb/base_handler.rb', line 58

def backtrace_lines
  full = ENV["FULL_BACKTRACE"]
  lines = @exception.backtrace
  if full
  else
    # This filtering business makes is hiding useful info.
    # Think it was needed for ERB but Tilt provides a better stack trace.
    # Commenting out for now.

    # filter out internal lines
    # removal_index = lines.find_index { |l| l =~ %r[lib/render_me_pretty] }
    # lines = lines[removal_index..-1] # remove leading lines above the lib/
    # render_me_pretty lines by keeping lines past the removal index
    # lines.reject! { |l| l =~ %r[lib/render_me_pretty] } # now filter out
    # render_me_pretty lines
    lines = lines[0..7] # keep 8 lines
  end
  lines[0] = lines[0].color(:red)

  # header
  lines.unshift "\nOriginal backtrace#{full ? "" : " (last 8 lines)"}:"
  # footer
  lines << "\nRe-run with FULL_BACKTRACE=1 to see all lines"
  lines.join("\n")
end

#handle ⇒ Object



9
10
11
12
# File 'lib/render_me_pretty/erb/base_handler.rb', line 9

def handle
  line_number = find_line_number
  pretty_trace(line_number, true) # returns StringIO
end

#pretty_trace(error_line_number, full_message = true) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/render_me_pretty/erb/base_handler.rb', line 14

def pretty_trace(error_line_number, full_message = true)
  io = StringIO.new

  message = full_message ? ": #{@exception.message}" : ""
  io.puts "#{@exception.class}#{message}".color(:red)

  pretty_path = template_path_with_error.sub(/^\.\//, "")
  io.puts "Error evaluating ERB template around line #{error_line_number.to_s.color(:red)} of: #{pretty_path}:"

  context = 5 # lines of context
  top, bottom = [error_line_number - context - 1, 0].max, error_line_number + context - 1

  lines = IO.read(template_path_with_error).split("\n")
  spacing = lines.size.to_s.size
  lines[top..bottom].each_with_index do |line_content, index|
    current_line_number = top + index + 1
    if current_line_number == error_line_number
      io.printf("%#{spacing}d %s\n".color(:red), current_line_number, line_content)
    else
      io.printf("%#{spacing}d %s\n", current_line_number, line_content)
    end
  end

  io.puts backtrace_lines
  io
end

#template_path_with_error ⇒ Object



41
42
43
# File 'lib/render_me_pretty/erb/base_handler.rb', line 41

def template_path_with_error
  error_in_layout? ? @layout_path : @path
end