Skip to content

Instantly share code, notes, and snippets.

@grodowski
Forked from steve9001/application.rb
Created November 16, 2016 15:29
Show Gist options
  • Save grodowski/813ce1edbf1e6e6d637da9ef092bd108 to your computer and use it in GitHub Desktop.
Save grodowski/813ce1edbf1e6e6d637da9ef092bd108 to your computer and use it in GitHub Desktop.
Rails middleware to provide information about errors during requests
module MyApp
class Application < Rails::Application
if Rails.env == 'test'
require 'diagnostic'
config.middleware.use(MyApp::DiagnosticMiddleware)
end
end
end
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment