class Cramp::ExceptionHandler

Constants

TEMPLATE

adapted from Django <djangoproject.com> Copyright © 2005, the Lawrence Journal-World Used under the modified BSD license: www.xfree86.org/3.3.6/COPYRIGHT2.html#5

Attributes

env[R]
exception[R]

Public Class Methods

new(env, exception) click to toggle source
# File lib/cramp/exception_handler.rb, line 30
def initialize(env, exception)
  @env = env
  @exception = exception
  @template = ERB.new(TEMPLATE)
end

Public Instance Methods

dump_exception() click to toggle source
# File lib/cramp/exception_handler.rb, line 36
def dump_exception
  string = "#{exception.class}: #{exception.message}\n"
  string << exception.backtrace.map { |l| "\t#{l}" }.join("\n")
  string
end
pretty() click to toggle source
# File lib/cramp/exception_handler.rb, line 42
def pretty
  req = Rack::Request.new(env)

  # This double assignment is to prevent an "unused variable" warning on
  # Ruby 1.9.3.  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 on
  # Ruby 1.9.3.  Yes, it is dumb, but I don't like Ruby yelling at me.
  frames = frames = exception.backtrace.map { |line|
    frame = OpenStruct.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