Permalink
Browse files

added dispatch hooks

  • Loading branch information...
mlomnicki committed Jan 29, 2011
1 parent 988ab78 commit 5e59f17b3de414ef98f3ec480c5215693abe92cf
Showing with 104 additions and 18 deletions.
  1. +4 −0 CHANGELOG.rdoc
  2. +1 −0 Gemfile
  3. +1 −1 Gemfile.lock
  4. +30 −0 lib/exceptioner.rb
  5. +22 −0 lib/exceptioner/dispatchable.rb
  6. +4 −13 lib/exceptioner/notifier.rb
  7. +4 −2 lib/exceptioner/transport/base.rb
  8. +38 −2 test/notifier_test.rb
View
@@ -1,5 +1,9 @@
= CHANGELOG
+== 0.1.0
+* global dispatch hook
+* dispatch hook for each transport
+
== 0.0.6 (24.01.2011)
* config.ingore specifies ignored exceptions
* support for Rails 2
View
@@ -1,3 +1,4 @@
source "http://rubygems.org"
gemspec
+
View
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- exceptioner (0.0.5)
+ exceptioner (0.0.6.pre1)
mail (~> 2.2)
xmpp4r (~> 0.5)
View
@@ -1,10 +1,12 @@
require 'exceptioner/core_ext/class/attribute'
require 'exceptioner/core_ext/module/attribute_accessors'
require 'exceptioner/core_ext/string/inflections'
+require 'exceptioner/dispatchable'
require 'exceptioner/version'
require 'exceptioner/railtie' if defined?(Rails::Railtie)
module Exceptioner
+ extend Dispatchable
class ExceptionerError < StandardError; end
@@ -53,6 +55,10 @@ def self.setup
yield self
end
+ def self.init
+ add_default_dispatchers
+ end
+
def self.mail
Transport::Mail
end
@@ -69,6 +75,30 @@ def self.config
self
end
+ def self.reset_dispatchers
+ clear_dispatchers
+ add_default_dispatchers
+ end
+
+ def self.add_default_dispatchers
+ disallow_development_environment
+ disallow_ignored_exceptions
+ end
+
+ def self.disallow_development_environment
+ dispatch do |exception|
+ ! development_environments.include?(environment_name)
+ end
+ end
+
+ def self.disallow_ignored_exceptions
+ dispatch do |exception|
+ ! Array(ignore).collect(&:to_s).include?(exception.class.name)
+ end
+ end
+
end
require 'exceptioner/support/rails2' if defined?(Rails::VERSION::MAJOR) && Rails::VERSION::MAJOR == 2
+
+Exceptioner.init
@@ -0,0 +1,22 @@
+module Exceptioner
+ module Dispatchable

This comment has been minimized.

Show comment
Hide comment
@apotonick

apotonick Feb 11, 2011

Why don't you use hooks here? Prost!

@apotonick

apotonick Feb 11, 2011

Why don't you use hooks here? Prost!

+
+ def dispatchers
+ @dispatchers ||= []
+ end
+
+ def clear_dispatchers
+ @dispatchers = []
+ end
+
+ def dispatch(clear = false, &block)
+ clear_dispatchers if clear
+ dispatchers << block
+ end
+
+ def run_dispatchers(exception)
+ dispatchers.all? { |dispatcher| dispatcher.call(exception) != false }
+ end
+
+ end
+end
@@ -2,10 +2,12 @@ module Exceptioner
class Notifier
def self.dispatch(exception, options = {})
- if dispatch_exception?(exception)
+ if config.run_dispatchers(exception)
options = determine_options(exception, options.dup)
determine_transports(options) do |transport|
- transport.deliver(options)
+ if transport.run_dispatchers(exception)
+ transport.deliver(options)
+ end
end
end
end
@@ -48,17 +50,6 @@ def self.config
Exceptioner
end
- def self.dispatch_exception?(exception)
- ! (config.development_environments.include?(config.environment_name) || ignore_exception?(exception))
- end
-
- def self.ignore_exception?(exception)
- if config.ignore
- # TODO: stringify config.ignore once
- Array(config.ignore).collect(&:to_s).include?(exception_class_name(exception))
- end
- end
-
# Determines class of exception.
def self.exception_class_name(exception)
exception.is_a?(Exception) ? exception.class.name : exception.to_s
@@ -1,6 +1,9 @@
+require 'exceptioner/dispatchable'
+
module Exceptioner::Transport
class Base
+ extend Exceptioner::Dispatchable
class_attribute :sender
@@ -10,11 +13,10 @@ class Base
class_attribute :subject
-
def self.deliver(options = {})
raise Exceptioner::ExceptionerError, 'Implement deliver class method in your Exceptioner::Transport::Base subclass'
end
-
+
protected
def self.default_options
{
View
@@ -8,11 +8,14 @@ class TestException < StandardError; end
class TestError < StandardError; end
def setup
- Exceptioner::Notifier.stubs(:dispatch_exception).returns(true)
config.mail.recipients = %w[michal@example.net]
config.jabber.jabber_id = %w[jabber@example.net]
config.jabber.password = 'secret'
config.jabber.recipients = %w[michal@example.net]
+ config.ignore = []
+ config.reset_dispatchers
+ config.mail.clear_dispatchers
+ config.jabber.clear_dispatchers
mail_system.clear_deliveries
end
@@ -21,7 +24,6 @@ def test_deliver_exception_by_email
Exceptioner::Notifier.stubs(:transports).returns([:mail])
Exceptioner::Notifier.dispatch(exception)
assert_equal 1, mail_system.deliveries.size
-
end
def test_deliver_exception_by_jabber
@@ -48,5 +50,39 @@ def test_ignores_specified_exceptions_given_by_class
Exceptioner::Notifier.dispatch(exception)
assert_equal 0, mail_system.deliveries.size
end
+
+ def test_run_global_dispatch
+ exception = get_exception(TestError)
+ object = mock()
+ object.expects(:do_something).with(exception)
+ Exceptioner.config.dispatch do |exception|
+ object.do_something(exception)
+ end
+ Exceptioner::Notifier.dispatch(exception)
+ end
+
+ def test_run_dispatch_for_transport
+ exception = get_exception(TestError)
+ Exceptioner::Notifier.stubs(:transports).returns([:jabber])
+ Exceptioner::Transport::Jabber.stubs(:deliver)
+ object = mock()
+ object.expects(:do_something).with(exception)
+ Exceptioner.config.jabber.dispatch do |exception|
+ object.do_something(exception)
+ end
+ Exceptioner::Notifier.dispatch(exception)
+ end
+
+ def test_breaks_if_returned_false_from_dispatch
+ exception = get_exception(TestError)
+ Exceptioner::Notifier.stubs(:transports).returns([:mail])
+ object = mock()
+ object.expects(:do_something).with(exception).returns(false)
+ Exceptioner.config.mail.dispatch do |exception|
+ object.do_something(exception)
+ end
+ Exceptioner::Notifier.dispatch(exception)
+ end
+
end

0 comments on commit 5e59f17

Please sign in to comment.