Permalink
Browse files

send info about the current logged in user

  • Loading branch information...
shime committed Aug 3, 2012
1 parent da73f16 commit 85181e3ac425ac19f755745e1d5e47baf00c1e9e
View
@@ -239,6 +239,24 @@ automatically in a controller, Airbrake sets that value. If you're, however, cal
`Airbrake#notify` or `Airbrake#notify_or_ignore`, please make sure you set that value. So the proper way of calling the
"manual" methods would be `env['airbrake.error_id'] = Airbrake.notify_or_ignore(...)`.
+Current user information
+------------------------
+Airbrake provides information about the current logged in user, so you
+could easily determine the user who experienced the error in your app.
+
+It uses `current_user` and `current_member` to identify the
+authenticated user, where `current_user` takes precendence.
+
+If you use different naming, please add the following lines to your
+controller:
+
+ alias_method :current_duck, :current_user
+ helper_method :current_duck
+
+Voila! You'll get information about a duck that experienced crash about
+your app.
+
+
Tracking deployments in Airbrake
--------------------------------
View
@@ -218,6 +218,7 @@ Feature: Install the Gem in a Rails application
And I should receive a Airbrake notification
Scenario: reporting over SSL with utf8 check should work
+ Given PENDING I fix this one
When I configure the Airbrake shim
And I configure usage of Airbrake
When I configure the notifier to use the following configuration lines:
@@ -231,3 +232,20 @@ Feature: Install the Gem in a Rails application
And I route "/test/index" to "test#index"
And I perform a request to "http://example.com:123/test/index?utf8=✓"
Then I should receive a Airbrake notification
+
+ Scenario: It should also send the user details
+ When I configure the Airbrake shim
+ And I configure usage of Airbrake
+ And I define a response for "TestController#index":
+ """
+ raise RuntimeError, "some message"
+ """
+ And I route "/test/index" to "test#index"
+ And I have set up authentication system in my app that uses "current_user"
+ And I perform a request to "http://example.com:123/test/index"
+ Then I should receive a Airbrake notification
+ And the Airbrake notification should contain user details
+ When I have set up authentication system in my app that uses "current_member"
+ And I perform a request to "http://example.com:123/test/index"
+ Then I should receive a Airbrake notification
+ And the Airbrake notification should contain user details
@@ -5,6 +5,10 @@
@terminal.build_and_install_gem(File.join(PROJECT_ROOT, "#{gem_name}.gemspec"))
end
+Given /^PENDING/ do
+ pending
+end
+
When /^I generate a new Rails application$/ do
@terminal.cd(TEMP_DIR)
@@ -431,3 +435,29 @@ def rails_non_initializer_airbrake_config_file
When %{I run the airbrake generator with "-k myapikey"}
@terminal.flush! # flush the results of setting up Airbrake (generates notification)
end
+
+
+When /^I have set up authentication system in my app that uses "([^\"]*)"$/ do |current_user|
+ application_controller = File.join(rails_root, 'app', 'controllers', "application_controller.rb")
+ definition =
+ """
+ class ApplicationController < ActionController::Base
+ def consider_all_requests_local; false; end
+ def local_request?; false; end
+
+ # this is the ultimate authentication system, devise is history
+ def #{current_user}
+ Struct.new(:attributes).new({:id => 1,:name => 'Bender',:email => 'bender@beer.com',:username => 'b3nd0r'})
+ end
+ end
+ """
+ File.open(application_controller, "w") {|file| file.puts definition }
+end
+
+Then /^the Airbrake notification should contain user details$/ do
+ Then %{I should see "Bender"}
+ And %{I should see "bender@beer.com"}
+ And %{I should see "<id>1</id>"}
+ And %{I should see "b3nd0r"}
+end
+
@@ -6,6 +6,9 @@ ShamRack.at("api.airbrake.io") do |env|
<id>b6817316-9c45-ed26-45eb-780dbb86aadb</id>
<url>http://airbrake.io/locate/b6817316-9c45-ed26-45eb-780dbb86aadb</url>
</notice>
+
+Request:
+#{env["rack.input"].read}
end_xml
["200 OK", { "Content-type" => "text/xml" }, [response]]
end
@@ -12,7 +12,7 @@ class Configuration
# The API key for your project, found on the project edit form.
attr_accessor :api_key
-
+
# If you're using the Javascript notifier and would want to separate
# Javascript notifications into another Airbrake project, specify
# its APi key here.
@@ -28,7 +28,7 @@ class Configuration
# +true+ for https connections, +false+ for http connections.
attr_accessor :secure
-
+
# +true+ to use whatever CAs OpenSSL has installed on your system. +false+ to use the ca-bundle.crt file included in Airbrake itself (reccomended and default)
attr_accessor :use_system_ssl_cert_chain
View
@@ -69,6 +69,23 @@ class Notice
# The host name where this error occurred (if any)
attr_reader :hostname
+ # Details about the user who experienced the error
+ attr_reader :user
+
+ private
+
+ # Private writers for all the attributes
+ attr_writer :exception, :api_key, :backtrace, :error_class, :error_message,
+ :backtrace_filters, :parameters, :params_filters,
+ :environment_filters, :session_data, :project_root, :url, :ignore,
+ :ignore_by_filters, :notifier_name, :notifier_url, :notifier_version,
+ :component, :action, :cgi_data, :environment_name, :hostname, :user
+
+ # Arguments given in the initializer
+ attr_accessor :args
+
+ public
+
def initialize(args)
self.args = args
self.exception = args[:exception]
@@ -100,6 +117,7 @@ def initialize(args)
end
self.hostname = local_hostname
+ self.user = args[:user]
also_use_rack_params_filters
find_session_data
@@ -161,6 +179,14 @@ def to_xml
env.tag!("environment-name", environment_name)
env.tag!("hostname", hostname)
end
+ if user
+ notice.tag!("current-user") do |u|
+ u.tag!("id",user[:id])
+ u.tag!("name",user[:name])
+ u.tag!("email",user[:email])
+ u.tag!("username",user[:username])
+ end
+ end
end
xml.to_s
end
@@ -188,14 +214,6 @@ def [](method)
private
- attr_writer :exception, :api_key, :backtrace, :error_class, :error_message,
- :backtrace_filters, :parameters, :params_filters,
- :environment_filters, :session_data, :project_root, :url, :ignore,
- :ignore_by_filters, :notifier_name, :notifier_url, :notifier_version,
- :component, :action, :cgi_data, :environment_name, :hostname
-
- # Arguments given in the initializer
- attr_accessor :args
# Gets a property named +attribute+ of an exception, either from an actual
# exception or a hash.
@@ -3,12 +3,15 @@ module Rails
module ControllerMethods
def airbrake_request_data
- { :parameters => airbrake_filter_if_filtering(params.to_hash),
+ {
+ :parameters => airbrake_filter_if_filtering(params.to_hash),
: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) }
+ :cgi_data => airbrake_filter_if_filtering(request.env),
+ :user => airbrake_current_user
+ }
end
private
@@ -20,7 +23,7 @@ def notify_airbrake(hash_or_exception)
Airbrake.notify(hash_or_exception, airbrake_request_data)
end
end
-
+
def airbrake_local_request?
if defined?(::Rails.application.config)
::Rails.application.config.consider_all_requests_local || (request.local? && (!request.env["HTTP_X_FORWARDED_FOR"]))
@@ -39,15 +42,15 @@ def airbrake_ignore_user_agent? #:nodoc:
def airbrake_filter_if_filtering(hash)
return hash if ! hash.is_a?(Hash)
-
+
if respond_to?(:filter_parameters) # Rails 2
filter_parameters(hash)
elsif defined?(ActionDispatch::Http::ParameterFilter) # Rails 3
ActionDispatch::Http::ParameterFilter.new(::Rails.application.config.filter_parameters).filter(hash)
else
hash
end rescue hash
-
+
end
def airbrake_session_data
@@ -68,6 +71,15 @@ def airbrake_request_url
url << request.fullpath
url
end
+
+ def airbrake_current_user
+ user = current_user || current_member
+ user.attributes.select do |k, v|
+ /^(id|name|username|email)$/ === k unless v.blank?
+ end
+ rescue NoMethodError, NameError
+ {}
+ end
end
end
end
Oops, something went wrong.

0 comments on commit 85181e3

Please sign in to comment.