cschneid / sinatra-book

OFFICAL REPO MOVED TO /sinatra/sinatra-book Tutorial + Cookbook

This URL has Read+Write access

neall (author)
Tue Jan 13 05:46:20 -0800 2009
cschneid (committer)
Wed Jan 14 15:24:09 -0800 2009
sinatra-book / book / ErrorHandling.markdown
100755 52 lines (37 sloc) 1.28 kb

Error Handling

not_found

Remember: These are run inside the Sinatra::EventContext which means you get all the goodies is has to offer (i.e. haml, erb, :halt, etc.)

Whenever NotFound is raised this will be called

not_found do
  'This is nowhere to be found'
end

error

By default error will catch Sinatra::ServerError

Sinatra will pass you the error via the ‘sinatra.error’ in request.env

error do
  'Sorry there was a nasty error - ' + request.env['sinatra.error'].name
end

Custom error mapping:

error MyCustomError do
  'So what happened was...' + request.env['sinatra.error'].message
end

then if this happens:

get '/' do
  raise MyCustomError, 'something bad'
end

you gets this:

So what happened was... something bad

Additional Information

Because Sinatra give you a default not_found and error do :production that are secure. If you want to customize only for :production but want to keep the friendly helper screens for :development then do this:

configure :production do
  not_found do
    "We're so sorry, but we don't what this is"
  end

  error do
    "Something really nasty happened.  We're on it!"
  end
end