class Sinatra::ShowExceptions

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.

Public Class Methods

new(app) click to toggle source
   # File lib/sinatra/show_exceptions.rb
15 def initialize(app)
16   @app      = app
17   @template = ERB.new(TEMPLATE)
18 end

Public Instance Methods

call(env) click to toggle source
   # File lib/sinatra/show_exceptions.rb
20 def call(env)
21   @app.call(env)
22 rescue Exception => e
23   errors, env["rack.errors"] = env["rack.errors"], @@eats_errors
24 
25   if prefers_plain_text?(env)
26     content_type = "text/plain"
27     body = [dump_exception(e)]
28   else
29     content_type = "text/html"
30     body = pretty(env, e)
31   end
32 
33   env["rack.errors"] = errors
34 
35   [500,
36    {"Content-Type" => content_type,
37     "Content-Length" => Rack::Utils.bytesize(body.join).to_s},
38    body]
39 end

Private Instance Methods

frame_class(frame) click to toggle source
   # File lib/sinatra/show_exceptions.rb
48 def frame_class(frame)
49   if frame.filename =~ /lib\/sinatra.*\.rb/
50     "framework"
51   elsif (defined?(Gem) && frame.filename.include?(Gem.dir)) ||
52         frame.filename =~ /\/bin\/(\w+)$/
53     "system"
54   else
55     "app"
56   end
57 end
prefers_plain_text?(env) click to toggle source
   # File lib/sinatra/show_exceptions.rb
43 def prefers_plain_text?(env)
44   !(Request.new(env).preferred_type("text/plain","text/html") == "text/html") &&
45   [/curl/].index{|item| item =~ env["HTTP_USER_AGENT"]}
46 end