Reputation: 17173
I would like to define an error block (or something) that would return all exceptions formatted in JSON somehow plus returning the standard http code (e.g. 404 for not found, 303 for auth errors etc).
Something like:
error do
e = env['sinatra.error']
json :result => 'error', :message => e.message
end
Thanks!
Upvotes: 4
Views: 4387
Reputation: 511
This should work:
require 'sinatra'
require 'json'
# This is needed for testing, otherwise the default
# error handler kicks in
set :environment, :production
error do
content_type :json
status 400 # or whatever
e = env['sinatra.error']
{:result => 'error', :message => e.message}.to_json
end
get '/' do
raise 'hell'
end
Test it with curl to see that it works.
Upvotes: 13