Gem of HTTP error status code class pack
Add this line to your application's Gemfile:
gem 'sinatra-errorcodes'
Or install it yourself as:
$ gem install sinatra-errorcodes
To use this gem, at first, you need to register Sinatra::Errorcodes
in your configure block and call handle_errorstatus
which wraps Sinatra's error handling block.
class AppController < Sinatra::Base
configure do
register Sinatra::Errorcodes
end
end
Now you can use a set of HTTPError
class which contains HTTP status code and its message.
You can use halt_with_errors
option which is false by default.
class AppController < Sinatra::Base
configure do
register Sinatra::Errorcodes
set :halt_with_errors, true
end
end
With the option, errors thrown with HTTPError
class are caught automatically and call halt
.
...
get '/badrequest'
raise HTTPError::BadRequest
end
get '/error'
raise HTTPError::InternalServerError
end
# You can also raise an exception with your own custom message.
get '/custom'
raise HTTPError::InternalServerError, 'Custom error message'
end
...
Install gems
$ bundle install
and run it
$ cd example
$ bundle exec rackup -p 3000
$ bundle exec rake spec
PRs accepted.
The gem is available as open source under the terms of the MIT License.