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

Public Instance Methods

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

Private Instance Methods

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