Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Extract request info from rack env

  • Loading branch information...
commit 47908c93f2e94e99480aec474327fe859d67c408 1 parent 81437f0
@jferris jferris authored
View
16 lib/hoptoad_notifier/notice.rb
@@ -68,7 +68,7 @@ def initialize(args)
self.exception = args[:exception]
self.api_key = args[:api_key]
self.project_root = args[:project_root]
- self.url = args[:url]
+ self.url = args[:url] || rack_env(:url)
self.notifier_name = args[:notifier_name]
self.notifier_version = args[:notifier_version]
@@ -78,12 +78,12 @@ def initialize(args)
self.ignore_by_filters = args[:ignore_by_filters] || []
self.backtrace_filters = args[:backtrace_filters] || []
self.params_filters = args[:params_filters] || []
- self.parameters = args[:parameters] || {}
+ self.parameters = args[:parameters] || rack_env(:params) || {}
self.component = args[:component] || args[:controller]
self.action = args[:action]
self.environment_name = args[:environment_name]
- self.cgi_data = args[:cgi_data]
+ self.cgi_data = args[:cgi_data] || args[:rack_env]
self.backtrace = Backtrace.parse(exception_attribute(:backtrace, caller), :filters => self.backtrace_filters)
self.error_class = exception_attribute(:error_class) {|exception| exception.class.name }
self.error_message = exception_attribute(:error_message, 'Notification') do |exception|
@@ -291,5 +291,15 @@ def xml_vars_for(builder, hash)
end
end
end
+
+ def rack_env(method)
+ rack_request.send(method) if rack_request
+ end
+
+ def rack_request
+ @rack_request ||= if args[:rack_env]
+ ::Rack::Request.new(args[:rack_env])
+ end
+ end
end
end
View
4 lib/hoptoad_notifier/rack.rb
@@ -8,12 +8,12 @@ def call(env)
begin
response = @app.call(env)
rescue Exception => raised
- HoptoadNotifier.notify_or_ignore(raised)
+ HoptoadNotifier.notify_or_ignore(raised, :rack_env => env)
raise
end
if env['rack.exception']
- HoptoadNotifier.notify_or_ignore(env['rack.exception'])
+ HoptoadNotifier.notify_or_ignore(env['rack.exception'], :rack_env => env)
end
response
View
26 test/notice_test.rb
@@ -143,7 +143,7 @@ def stub_request(attrs = {})
end
should "use the caller as the backtrace for an exception without a backtrace" do
- filters = HoptoadNotifier.configuration.backtrace_filters
+ filters = HoptoadNotifier::Configuration.new.backtrace_filters
backtrace = HoptoadNotifier::Backtrace.parse(caller, :filters => filters)
notice = build_notice(:exception => StandardError.new('error'), :backtrace => nil)
@@ -338,6 +338,30 @@ def stub_request(attrs = {})
end
end
+ should "extract data from a rack environment hash" do
+ # def hoptoad_request_data
+ # { :parameters => hoptoad_filter_if_filtering(params.to_hash),
+ # :session_data => hoptoad_session_data,
+ # :controller => params[:controller],
+ # :action => params[:action],
+ # :url => hoptoad_request_url,
+ # :cgi_data => hoptoad_filter_if_filtering(request.env),
+ # :environment_vars => hoptoad_filter_if_filtering(ENV) }
+ # end
+ # TODO: extract session data
+ # TODO: extract controller
+ # TODO: extract action
+ url = "https://subdomain.happylane.com:100/test/file.rb?var=value&var2=value2"
+ parameters = { 'var' => 'value', 'var2' => 'value2' }
+ env = Rack::MockRequest.env_for(url)
+
+ notice = build_notice(:rack_env => env)
+
+ assert_equal url, notice.url
+ assert_equal parameters, notice.parameters
+ assert_equal 'GET', notice.cgi_data['REQUEST_METHOD']
+ end
+
def assert_accepts_exception_attribute(attribute, args = {}, &block)
exception = build_exception
block ||= lambda { exception.send(attribute) }
View
14 test/rack_test.rb
@@ -16,25 +16,29 @@ class RackTest < Test::Unit::TestCase
HoptoadNotifier.stubs(:notify_or_ignore)
exception = build_exception
+ environment = { 'key' => 'value' }
app = lambda do |env|
raise exception
end
begin
stack = HoptoadNotifier::Rack.new(app)
- stack.call({})
+ stack.call(environment)
rescue Exception => raised
assert_equal exception, raised
else
flunk "Didn't raise an exception"
end
- assert_received(HoptoadNotifier, :notify_or_ignore) { |expect| expect.with(exception) }
+ assert_received(HoptoadNotifier, :notify_or_ignore) do |expect|
+ expect.with(exception, :rack_env => environment)
+ end
end
should "deliver an exception in rack.exception" do
HoptoadNotifier.stubs(:notify_or_ignore)
exception = build_exception
+ environment = { 'key' => 'value' }
response = [200, {}, ['okay']]
app = lambda do |env|
@@ -43,10 +47,12 @@ class RackTest < Test::Unit::TestCase
end
stack = HoptoadNotifier::Rack.new(app)
- actual_response = stack.call({})
+ actual_response = stack.call(environment)
assert_equal response, actual_response
- assert_received(HoptoadNotifier, :notify_or_ignore) { |expect| expect.with(exception) }
+ assert_received(HoptoadNotifier, :notify_or_ignore) do |expect|
+ expect.with(exception, :rack_env => environment)
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.