From 55f178e4a258696a64e379ffeac506aa9961894d Mon Sep 17 00:00:00 2001 From: Rob Sanheim Date: Mon, 4 Jan 2010 13:35:28 -0600 Subject: [PATCH] Allow setting the ignore list off for exceptions on a per exception level. --- CHANGELOG.markdown | 4 ++++ .../exception_notification_example.rb | 19 +++++++++++++++++++ lib/chatterbox/exception_notification.rb | 18 ++++++++++++++++-- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.markdown b/CHANGELOG.markdown index e463bda..139c7b3 100644 --- a/CHANGELOG.markdown +++ b/CHANGELOG.markdown @@ -1,5 +1,9 @@ # Changelog +### 0.8.5 + - Allow setting the ignore list off for exceptions on a per + exception level. + ### 0.8.4 - Log ignored exceptions diff --git a/examples/lib/chatterbox/exception_notification_example.rb b/examples/lib/chatterbox/exception_notification_example.rb index ad24bb5..293995f 100644 --- a/examples/lib/chatterbox/exception_notification_example.rb +++ b/examples/lib/chatterbox/exception_notification_example.rb @@ -17,6 +17,12 @@ Chatterbox::ExceptionNotification.configure { |c| c.ignore = Chatterbox::ExceptionNotification.default_ignored_exceptions } end + it "does not notify" do + Chatterbox::ExceptionNotification.configure { |c| c.ignore << RuntimeError } + Chatterbox.expects(:notify).never + Chatterbox::ExceptionNotification.handle(:exception => RuntimeError.new) + end + it "returns nil for explicit exception" do Chatterbox::ExceptionNotification.configure { |c| c.ignore << RuntimeError } Chatterbox::ExceptionNotification.handle(RuntimeError.new).should be_nil @@ -32,9 +38,22 @@ Chatterbox.logger.expects(:debug).once Chatterbox::ExceptionNotification.handle(RuntimeError.new) end + + describe "with the ignore list manually turned off" do + it "notifies" do + Chatterbox::ExceptionNotification.configure { |c| c.ignore << RuntimeError } + Chatterbox.expects(:notify) + Chatterbox::ExceptionNotification.handle(:exception => RuntimeError.new, :use_ignore_list => false) + end + end end describe "when not on ignore list" do + it "notifies" do + Chatterbox.expects(:notify) + Chatterbox::ExceptionNotification.handle(:exception => Exception.new) + end + it "logs nothing" do Chatterbox.logger.expects(:debug).never Chatterbox::ExceptionNotification.handle(:exception => Exception.new) diff --git a/lib/chatterbox/exception_notification.rb b/lib/chatterbox/exception_notification.rb index 101f267..e2384fd 100644 --- a/lib/chatterbox/exception_notification.rb +++ b/lib/chatterbox/exception_notification.rb @@ -11,7 +11,7 @@ module ExceptionNotification # * Objects are simply treated as a 'summary' message were an exception may not be necessary def handle(args) hsh = normalize_to_hash(args) - return if on_ignore_list?(hsh[:exception]) + return if use_ignore_list?(hsh) && on_ignore_list?(hsh[:exception]) hsh = Extracter.wrap(hsh) hsh = RailsExtracter.wrap(hsh) hsh = Presenter.render(hsh) @@ -26,6 +26,14 @@ def normalize_to_hash(args) end end + # Check to see if we should use ignore list for this exception - + # first use the value from the passed in options, otherwise + # use the configuration value (which defaults to true) + def use_ignore_list?(hsh) + return hsh[:use_ignore_list] if hsh.key?(:use_ignore_list) + configuration.use_ignore_list + end + def on_ignore_list?(exception) ignored = configuration.ignore.include?(exception.class) || configuration.ignore.include?(exception.class.to_s) @@ -37,8 +45,14 @@ def log_ignored_exception(exception) Chatterbox.logger.debug { %[Chatterbox::ExceptionNotification ignoring exception: "#{exception}" as its on the ignore list] } end + # Default configuration for ExceptionNotification + # :ignore => array of exceptions to ignore by default + # :use_ignore_list => whether to use the ignore list - + # defaults to true, and can be overidden on per exception level def configuration - @configuration ||= OpenStruct.new(:ignore => default_ignored_exceptions) + @configuration ||= OpenStruct.new( + :ignore => default_ignored_exceptions, + :use_ignore_list => true) end # Configure ExceptionNotification