« Back to Index

Sinatra errors

View original Gist on GitHub

sinatra errors.rb

require "sinatra/base"

class Foo < Sinatra::Base
  def initialize
    configure
    super()
  end

  get "/" do
    "hello"
  end

  get "/err" do
    404 # shows no content (but go to /foo and you'll see Sinatra 404)
  end

  get "/fail" do
    fail "wat?" # this is rescued by main sinatra error method
  end

  error do
    # method typically used to show some kind of response to user
    # but not in our example here... instead we see what happens if this error method itself fails
    call_method_that_fails # depending on configure method being run: it should either show a safe Internal Server Error or revealing stack trace
  end

  private

  def configure
    self.class.tap do |s|
      s.configure { s.disable :show_exceptions, :raise_errors }
    end
  end

  def call_method_that_fails
    fail "wat!?" # shows different error page but still has backtrace?
  end
end