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, line 19
def initialize(app)
  @app      = app
  @template = ERB.new(TEMPLATE)
end

Public Instance Methods

call(env) click to toggle source
# File lib/sinatra/show_exceptions.rb, line 24
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"
    exception = dump_exception(e)
  else
    content_type = "text/html"
    exception = pretty(env, e)
  end

  env["rack.errors"] = errors

  # Post 893a2c50 in rack/rack, the #pretty method above, implemented in
  # Rack::ShowExceptions, returns a String instead of an array.
  body = Array(exception)

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

Private Instance Methods

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