Skip to content

IzumiSy/sinatra-errorcodes

Repository files navigation

Sinatra::Errorcodes

Gem Version CircleCI standard-readme compliant

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
  end
end

Now you can use a set of HTTPError class which contains HTTP status code and its message.

halt_with_errors

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
  
...

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.