Created — forked from steve9001/application.rb

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Rails middleware to provide information about errors during :js => true requests in test.log and to STDERR

View application.rb
1 2 3 4 5 6 7 8
module MyApp
class Application < Rails::Application
if Rails.env == 'test'
require 'diagnostic'
config.middleware.use(MyApp::DiagnosticMiddleware)
end
end
end
View application.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
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
View application.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
module Motorlot
class DiagnosticMiddleware
FILENAME = File.join(Rails.root, 'log', 'test.log')
 
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 ")
 
error = "\n\n --- BACKTRACE --- \n\n"
error += "#{e.class}\n#{e.message}\n#{trace}\n"
error += "\n\n --- BACKTRACE --- \n\n"
 
Logger.new(STDERR).error error
File.open(FILENAME, 'a') { |f| f.write(error) }
raise e
end
end
end
View application.rb
1 2 3 4 5 6
module MyApp
class Application < Rails::Application
require 'diagnostic_middleware'
config.middleware.use(Motorlot::DiagnosticMiddleware)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.