Method: Rack::ShowExceptions#pretty

Defined in:
lib/rack/show_exceptions.rb

#pretty(env, exception) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rack/show_exceptions.rb', line 80

def pretty(env, exception)
  req = Rack::Request.new(env)

  # This double assignment is to prevent an "unused variable" warning.
  # Yes, it is dumb, but I don't like Ruby yelling at me.
  path = path = (req.script_name + req.path_info).squeeze("/")

  # This double assignment is to prevent an "unused variable" warning.
  # Yes, it is dumb, but I don't like Ruby yelling at me.
  frames = frames = exception.backtrace.map { |line|
    frame = Frame.new
    if line =~ /(.*?):(\d+)(:in `(.*)')?/
      frame.filename = $1
      frame.lineno = $2.to_i
      frame.function = $4

      begin
        lineno = frame.lineno - 1
        lines = ::File.readlines(frame.filename)
        frame.pre_context_lineno = [lineno - CONTEXT, 0].max
        frame.pre_context = lines[frame.pre_context_lineno...lineno]
        frame.context_line = lines[lineno].chomp
        frame.post_context_lineno = [lineno + CONTEXT, lines.size].min
        frame.post_context = lines[lineno + 1..frame.post_context_lineno]
      rescue
      end

      frame
    else
      nil
    end
  }.compact

  template.result(binding)
end