Skip to content

Commit

Permalink
Extract request info from rack env
Browse files Browse the repository at this point in the history
  • Loading branch information
jferris committed Jan 28, 2010
1 parent 81437f0 commit 47908c9
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
16 changes: 13 additions & 3 deletions lib/hoptoad_notifier/notice.rb
Expand Up @@ -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]
Expand All @@ -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|
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions lib/hoptoad_notifier/rack.rb
Expand Up @@ -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
Expand Down
26 changes: 25 additions & 1 deletion test/notice_test.rb
Expand Up @@ -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)

Expand Down Expand Up @@ -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) }
Expand Down
14 changes: 10 additions & 4 deletions test/rack_test.rb
Expand Up @@ -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|
Expand All @@ -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

0 comments on commit 47908c9

Please sign in to comment.