Skip to content

Commit

Permalink
tests and improvements for agent/class ignoring
Browse files Browse the repository at this point in the history
  • Loading branch information
shime committed Nov 28, 2012
1 parent d77a128 commit 2a34eae
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 13 deletions.
20 changes: 19 additions & 1 deletion lib/exceptional/catcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ module Exceptional
class Catcher
class << self
def handle_with_controller(exception, controller=nil, request=nil)
if Config.should_send_to_api?
if Config.should_send_to_api? &&
!ignore?(exception, request)

data = ControllerExceptionData.new(exception, controller, request)
Remote.error(data)
else
Expand All @@ -29,6 +31,22 @@ def handle(exception, name=nil)
raise exception
end
end

def ignore?(exception, request)
ignore_class?(exception) || ignore_user_agent?(request)
end

def ignore_class?(exception)
Config.ignore_exceptions.any? do |exception_class|
exception_class === exception.class
end
end

def ignore_user_agent?(request)
Config.ignore_user_agents.any? do |user_agent|
user_agent === request.user_agent
end
end
end
end
end
14 changes: 8 additions & 6 deletions lib/exceptional/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ class << self
:disabled_by_default => %w(development test)
}

attr_accessor :api_key, :enabled
attr_accessor :http_proxy_host, :http_proxy_port, :http_proxy_username, :http_proxy_password, :ignored_agents, :ignored_exceptions
attr_accessor :api_key, :enabled, :http_proxy_host, :http_proxy_port,
:http_proxy_username, :http_proxy_password, :ignore_user_agents,
:ignore_exceptions

attr_writer :ssl

def load(config_file=nil)
Expand Down Expand Up @@ -68,12 +70,12 @@ def ssl?
@ssl ||= DEFAULTS[:ssl]
end

def ignored_agents
@ignored_agents || []
def ignore_user_agents
@ignore_user_agents ||= []
end

def ignored_exceptions
@ignored_exceptions || []
def ignore_exceptions
@ignore_exceptions ||= []
end

def remote_host
Expand Down
7 changes: 3 additions & 4 deletions lib/exceptional/integration/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
module ActionController
class Base
def rescue_action_with_exceptional(exception)
unless exception_handled_by_rescue_from?(exception) ||
Regexp.new(Exceptional::Config.ignored_agents.join('|')) =~ request.user_agent ||
Regexp.new(Exceptional::Config.ignored_exceptions.join('|')) =~ exception.class.to_s
unless exception_handled_by_rescue_from?(exception)
Exceptional::Catcher.handle_with_controller(exception, self, request)
Exceptional.context.clear!
end
Expand All @@ -21,9 +19,10 @@ def rescue_action_with_exceptional(exception)
protected :rescue_action

private

def exception_handled_by_rescue_from?(exception)
respond_to?(:handler_for_rescue) && handler_for_rescue(exception)
end
end
end
end
end
37 changes: 36 additions & 1 deletion spec/exceptional/catcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,41 @@
Exceptional::Remote.should_receive(:error).with(data)
Exceptional::Catcher.handle_with_controller(exception,controller,request)
end

describe "#ignore?" do
before do
@exception = mock('exception')
@controller = mock('controller')
@request = mock('request')
end

it "should check for ignored classes and agents" do
Exceptional::Catcher.should_receive(:ignore_class?).with(@exception)
Exceptional::Catcher.should_receive(:ignore_user_agent?).with(@request)
Exceptional::ControllerExceptionData.should_receive(:new).with(@exception,@controller,@request).and_return(data = mock('exception_data'))
Exceptional::Remote.should_receive(:error).with(data)
Exceptional::Catcher.handle_with_controller(@exception, @controller, @request)
end
it "should ignore exceptions by class name" do
request = mock("request")
exception = mock("exception")
exception.stub(:class).and_return("ignore_me")
exception.should_receive(:class)
Exceptional::Config.ignore_exceptions = ["ignore_me",/funky/]
Exceptional::Catcher.ignore_class?(exception).should be_true
funky_exception = mock("exception")
funky_exception.stub(:class).and_return("really_funky_exception")
funky_exception.should_receive(:class)
Exceptional::Catcher.ignore_class?(funky_exception).should be_true
end
it "should ignore exceptions by user agent" do
request = mock("request")
request.stub(:user_agent).and_return("botmeister")
request.should_receive(:user_agent)
Exceptional::Config.ignore_user_agents = [/bot/]
Exceptional::Catcher.ignore_user_agent?(request).should be_true
end
end
# it "handle_with_rack should create exception_data object and send json to the api"
# it "handle should create exception_data object and send json to the api"
end
Expand All @@ -34,4 +69,4 @@
# it "handle_with_rack should reraise the exception and not report it"
# it "handle should reraise the exception and not report it"
end
end
end
49 changes: 48 additions & 1 deletion spec/rails_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,60 @@ def raises_something
end
end

class IgnoreAgentController < ActionController::Base

class SomethingIgnored < StandardError; end

def raises_something
raise StandardError
end

def raise_but_ignore
Exceptional::Config.ingore_user_agents = "BOTMEISTER"
request.user_agent = "BOTMEISTER"
raise StandardError
end

def raise_but_ignore_with_regex
Exceptional::Config.ignore_exceptions = [/Ignored/]
raise SomethingIgnored
end
end

describe IgnoreAgentController do
before :each do
@controller = IgnoreAgentController.new
Exceptional::Config.stub!(:should_send_to_api?).and_return(true)
end

it "should send the exception if agent is not ignored" do
Exceptional::Remote.should_receive(:error)
send_request(:raises_something)
end

it "should not send the exception if agent is ignored" do
Exceptional::Remote.should_not_receive(:error)
expect { send_request(:raise_but_ignore)}.to raise_exception
end

it "should not send the exception if class is ignored with regex" do
Exceptional::Remote.should_not_receive(:error)
expect { send_request(:raise_but_ignore_with_regex)}.to raise_exception
end
end

describe TestingController do
before :each do
@controller = TestingController.new
end

it 'handle exception with Exceptional::Catcher' do
Exceptional::Catcher.should_receive(:handle_with_controller).with(an_instance_of(StandardError), @controller, an_instance_of(ActionController::TestRequest))
Exceptional::Catcher.should_receive(:handle_with_controller).
with(
an_instance_of(StandardError),
@controller,
an_instance_of(ActionController::TestRequest)
)
send_request(:raises_something)
end

Expand Down

0 comments on commit 2a34eae

Please sign in to comment.