Error handling in Sinatra like Grape
Ruby
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
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