Skip to content

Instantly share code, notes, and snippets.

@matan23
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matan23/8956626 to your computer and use it in GitHub Desktop.
Save matan23/8956626 to your computer and use it in GitHub Desktop.
Sinatra Exception Handling
class App < Sinatra::Base
configure :development do
set :raise_errors, true
#when set to true display a nice exception page for dev but prevent exception handler from working
set :show_exceptions, false
end
get '/someroute' do
MyModel.might_generate_an_exception
status 200
end
error ValidationError do
if settings.environment == :development
env['sinatra.error'].message
else
#I don't want to tell you what happened you just messed up!
halt 400
end
end
end
class ValidationError < Exception; end
class MyModel
if edge_case
raise ValidationError, 'Hey this is coming from my model class!'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment