Last active
August 29, 2015 13:56
-
-
Save matan23/8956626 to your computer and use it in GitHub Desktop.
Sinatra Exception Handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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