Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Error handling in Sinatra like Grape
Ruby
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib/sinatra
README.md
sinatra-error.gemspec

README.md

Sinatra::Error

Error handling in Sinatra like Grape.


Like in Grape, now you can do this in Sinatra

$ gem install sinatra-error

require 'sinatra'
require 'sinatra/error'

# parameters:
#   message => String or Hash with the output
#   status  => Integer (default  403)
#   format  => Symbol  (default  :json)

error!({ 'error' => 'unauthorized', 'detail' => 'login required' }, 401) unless logged?
    # {"error":"unauthorized","detail":"login required"}
error!('unauthorized', 401) unless logged?
    # {"error":"unauthorized"}
error!('unauthorized', 401, :txt) unless logged?
    # unauthorized
Something went wrong with that request. Please try again.