Skip to content

Instantly share code, notes, and snippets.

@Sjeanpierre
Forked from ahawkins/error_handling.rb
Last active August 29, 2015 14:16
Show Gist options
  • Save Sjeanpierre/1dc3da11ebf909f6c974 to your computer and use it in GitHub Desktop.
Save Sjeanpierre/1dc3da11ebf909f6c974 to your computer and use it in GitHub Desktop.
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