lib/airbrake/rails/controller_methods.rb in airbrake-4.1.0 vs lib/airbrake/rails/controller_methods.rb in airbrake-4.2.0
- old
+ new
@@ -8,11 +8,11 @@
:session_data => airbrake_filter_if_filtering(airbrake_session_data),
:controller => params[:controller],
:action => params[:action],
:url => airbrake_request_url,
:cgi_data => airbrake_filter_if_filtering(request.env),
- :user => airbrake_current_user
+ :user => airbrake_current_user || {}
}
end
private
@@ -42,26 +42,37 @@
def airbrake_filter_if_filtering(hash)
return hash if ! hash.is_a?(Hash)
if respond_to?(:filter_parameters) # Rails 2
filter_parameters(hash)
- elsif rails3?
+ elsif rails_3_or_4?
filter_rails3_parameters(hash)
else
hash
end
end
- def rails3?
- defined?(::Rails.version) && ::Rails.version =~ /\A3/
+ def rails_3_or_4?
+ defined?(::Rails.version) && ::Rails.version =~ /\A[34]/
end
def filter_rails3_parameters(hash)
ActionDispatch::Http::ParameterFilter.new(
- ::Rails.application.config.filter_parameters).filter(hash)
+ ::Rails.application.config.filter_parameters
+ ).filter(recursive_stringify_keys(hash))
end
+ def recursive_stringify_keys(hash)
+ hash = hash.stringify_keys
+ hash.each do |k, v|
+ if v.is_a?(Hash)
+ hash[k] = v.respond_to?(:stringify_keys) ? recursive_stringify_keys(v) : nil # Rack::Session::Abstract::SessionHash has a stringify_keys method we should not call
+ end
+ end
+ hash
+ end
+
def airbrake_session_data
if session
if session.respond_to?(:to_hash)
session.to_hash
else
@@ -77,23 +88,37 @@
unless [80, 443].include?(request.port)
url << ":#{request.port}"
end
- url << request.fullpath
- url
+ URI.join(url, request.fullpath).to_s
end
def airbrake_current_user
- user = begin current_user rescue current_member end
- h = {}
- return h if user.nil?
- Airbrake.configuration.user_attributes.map(&:to_sym).each do |attr|
- h[attr.to_sym] = user.send(attr) if user.respond_to? attr
+ user = fetch_user
+
+ if user
+ Airbrake.configuration.user_attributes.map(&:to_sym).inject({}) do |hsh, attr|
+ hsh[attr.to_sym] = user.send(attr) if user.respond_to? attr
+ hsh
+ end
end
- h
- rescue NoMethodError, NameError
- {}
+ end
+
+ def fetch_user
+ if defined?(current_user)
+ current_user
+ elsif defined?(current_member)
+ current_member
+ else
+ nil
+ end
+ ensure
+ # The Airbrake middleware is first in the chain, before ActiveRecord::ConnectionAdapters::ConnectionManagement
+ # kicks in to do its thing. This can cause the connection pool to run out of connections.
+ if defined?(ActiveRecord) && ActiveRecord::Base.respond_to?(:connection_pool)
+ ActiveRecord::Base.connection_pool.release_connection
+ end
end
end
end
end