Sinatra::Errorcodes
Gem of HTTP error status code class pack
Table of Contents
Installation
Add this line to your application's Gemfile:
gem 'sinatra-errorcodes'
Or install it yourself as:
$ gem install sinatra-errorcodes
Usage
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
handle_errorstatus # Need to add this.
end
end
Now you can use a set of HTTPError class in order to halt manually with a specific HTTP status code and its message like below. The all process of these exception raising is done by handle_errorstatus you have just added above.
...
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
...
Example
Install gems
$ bundle install
and run it
$ cd example
$ bundle exec rackup -p 3000
Test
$ bundle exec rake spec
Contribute
PRs accepted.
License
The gem is available as open source under the terms of the MIT License.