Skip to content

Use Module#prepend instead of alias_chaining for rails exceptions #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/influxdb-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ def rescue_and_reraise(&block)
transmit_unless_ignorable(e)
raise(e)
end

def safely_prepend(module_name, opts = {})
return if opts[:to].nil? || opts[:from].nil?
if opts[:to].respond_to?(:prepend, true)
opts[:to].send(:prepend, opts[:from].const_get(module_name))
else
opts[:to].send(:include, opts[:from].const_get("Old" + module_name))
end
end
end
end
end
12 changes: 11 additions & 1 deletion lib/influxdb/rails/middleware/hijack_render_exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ module InfluxDB
module Rails
module Middleware
module HijackRenderException
def render_exception(env, e)
controller = env["action_controller.instance"]
request_data = controller.try(:influxdb_request_data) || {}
unless InfluxDB::Rails.configuration.ignore_user_agent?(request_data[:user_agent])
InfluxDB::Rails.report_exception_unless_ignorable(e, request_data)
end
super
end
end

module OldHijackRenderException
def self.included(base)
base.send(:alias_method_chain, :render_exception, :influxdb)
end
Expand All @@ -18,4 +29,3 @@ def render_exception_with_influxdb(env, e)
end
end
end

10 changes: 8 additions & 2 deletions lib/influxdb/rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,18 @@ class Railtie < ::Rails::Railtie

if defined?(::ActionDispatch::DebugExceptions)
require 'influxdb/rails/middleware/hijack_render_exception'
::ActionDispatch::DebugExceptions.send(:include, InfluxDB::Rails::Middleware::HijackRenderException)
exceptions_class = ::ActionDispatch::DebugExceptions
elsif defined?(::ActionDispatch::ShowExceptions)
require 'influxdb/rails/middleware/hijack_render_exception'
::ActionDispatch::ShowExceptions.send(:include, InfluxDB::Rails::Middleware::HijackRenderException)
exceptions_class = ::ActionDispatch::ShowExceptions
end

InfluxDB::Rails.safely_prepend(
"HijackRenderException",
:from => InfluxDB::Rails::Middleware,
:to => exceptions_class
)

if defined?(ActiveSupport::Notifications)
ActiveSupport::Notifications.subscribe "process_action.action_controller" do |name, start, finish, id, payload|
if InfluxDB::Rails.configuration.instrumentation_enabled && ! InfluxDB::Rails.configuration.ignore_current_environment?
Expand Down