Last active
December 17, 2015 22:59
-
-
Save ahawkins/5686180 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra' | |
require 'multi_json' | |
class App < Sinatra::Application | |
configure do | |
# Don't log them. We'll do that ourself | |
set :dump_errors, false | |
# Don't capture any errors. Throw them up the stack | |
set :raise_errors, true | |
# Disable internal middleware for presenting errors | |
# as useful HTML pages | |
set :show_exceptions, false | |
end | |
end | |
class ExceptionHandling | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
begin | |
@app.call env | |
rescue => ex | |
env['rack.errors'].puts ex | |
env['rack.errors'].puts ex.backtrace.join("\n") | |
env['rack.errors'].flush | |
hash = { :message => ex.to_s } | |
hash[:backtrace] ex.backtrace if RACK_ENV['development'] | |
[500, {'Content-Type' => 'application/json'}, [MultiJson.dump(hash)]] | |
end | |
end | |
end | |
use ExceptionHandling | |
run App |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this