Class: Sinatra::ShowExceptions

Inherits:
Rack::ShowExceptions
  • Object
show all
Defined in:
lib/sinatra/show_exceptions.rb

Overview

Sinatra::ShowExceptions catches all exceptions raised from the app it wraps. It shows a useful backtrace with the sourcefile and clickable context, the whole Rack environment and the request data.

Be careful when you use this on public-facing sites as it could reveal information helpful to attackers.

Constant Summary collapse

@@eats_errors =
Object.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ShowExceptions

Returns a new instance of ShowExceptions.



17
18
19
# File 'lib/sinatra/show_exceptions.rb', line 17

def initialize(app)
  @app = app
end

Class Method Details

.flushObject



14
# File 'lib/sinatra/show_exceptions.rb', line 14

def @@eats_errors.flush(*) end

.putsObject



15
# File 'lib/sinatra/show_exceptions.rb', line 15

def @@eats_errors.puts(*) end

Instance Method Details

#call(env) ⇒ Object



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/sinatra/show_exceptions.rb', line 21

def call(env)
  @app.call(env)
rescue Exception => e
  errors, env["rack.errors"] = env["rack.errors"], @@eats_errors

  if prefers_plain_text?(env)
    content_type = "text/plain"
    body = dump_exception(e)
  else
    content_type = "text/html"
    body = pretty(env, e)
  end

  env["rack.errors"] = errors

  [
    500,
    {
      "Content-Type" => content_type,
      "Content-Length" => body.bytesize.to_s
    },
    [body]
  ]
end

#pretty(env, exception) ⇒ Object

Pulled from Rack::ShowExceptions in order to override TEMPLATE. If Rack provides another way to override, this could be removed in the future.



49
50
51
52
53
54
55
56
57
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
83
# File 'lib/sinatra/show_exceptions.rb', line 49

def pretty(env, exception)
  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