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
17 def initialize(app)
18   @app = app
19 end

Public Instance Methods

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

Private Instance Methods

bad_request?(e) click to toggle source
   # File lib/sinatra/show_exceptions.rb
52 def bad_request?(e)
53   Sinatra::BadRequest === e
54 end
frame_class(frame) click to toggle source
   # File lib/sinatra/show_exceptions.rb
61 def frame_class(frame)
62   if frame.filename =~ %r{lib/sinatra.*\.rb}
63     "framework"
64   elsif (defined?(Gem) && frame.filename.include?(Gem.dir)) ||
65         frame.filename =~ %r{/bin/(\w+)\z}
66     "system"
67   else
68     "app"
69   end
70 end
prefers_plain_text?(env) click to toggle source
   # File lib/sinatra/show_exceptions.rb
56 def prefers_plain_text?(env)
57   !(Request.new(env).preferred_type("text/plain","text/html") == "text/html") &&
58   [/curl/].index { |item| item =~ env["HTTP_USER_AGENT"] }
59 end