Created
December 7, 2011 16:18
-
-
Save steve9001/1443408 to your computer and use it in GitHub Desktop.
Rails middleware to provide information about errors during requests
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
module MyApp | |
class Application < Rails::Application | |
if Rails.env == 'test' | |
require 'diagnostic' | |
config.middleware.use(MyApp::DiagnosticMiddleware) | |
end | |
end | |
end |
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
module MyApp | |
class DiagnosticMiddleware | |
FILENAME = 'diagnostic.txt' | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
return @app.call(env) | |
rescue StandardError => e | |
trace = e.backtrace.select{ |l|l.start_with?(Rails.root.to_s) }.join("\n ") | |
msg = "#{e.class}\n#{e.message}\n#{trace}\n" | |
File.open(FILENAME, 'a') { |f| f.write msg } | |
raise e | |
end | |
end | |
end |
It didn't work for me until I changed it to rescue Exception instead of StandardError.
application.rb
should be:
if Rails.env == 'test'
require File.expand_path('../diagnostic', __FILE__)
config.middleware.use(MyApp::DiagnosticMiddleware)
end
I happened to handle this a little bit differently -- by detecting RoutingErrors on image paths: https://gist.github.com/mcmire/68cd9c74ba765a2d5dfb14abf58409aa
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the same here, i would also like to have it as a spec, so it does not work for me.