summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAzul <azul@riseup.net>2016-09-20 09:14:43 +0200
committerAzul <azul@riseup.net>2016-09-21 12:48:06 +0200
commitdd71240fe4f4f968b9b687917cb6d7ad5812ba48 (patch)
treeda0e5718dddba4f435e0ab8f7c596c438ffab403
parenta1c7d68b05f142322a190b450971d27c076310a9 (diff)
rescue and track exceptions in handler chain
-rw-r--r--lib/nickserver/handler_chain.rb28
-rw-r--r--test/unit/handler_chain_test.rb23
2 files changed, 48 insertions, 3 deletions
diff --git a/lib/nickserver/handler_chain.rb b/lib/nickserver/handler_chain.rb
index a0eba4d..afc24a5 100644
--- a/lib/nickserver/handler_chain.rb
+++ b/lib/nickserver/handler_chain.rb
@@ -5,8 +5,11 @@
# will call the handlers with the given args until one of them returns a result
# that is truethy (i.e. not false or nil).
#
-# Extracted from the dispatcher so we can also handle exceptions here in the
-# future.
+# You can specify exception classes to rescue with
+# handler_chain.continue_on ErrorClass1, ErrorClass2
+# These exceptions will be rescued and tracked. The chain will proceed even if
+# one handler raised the given exception. Afterwards you can inspect them with
+# handler_chain.rescued_exceptions
#
module Nickserver
@@ -14,13 +17,32 @@ module Nickserver
def initialize(*handlers)
@handlers = handlers
+ @exceptions_to_rescue = []
+ @rescued_exceptions = []
+ end
+
+ def continue_on(*exceptions)
+ self.exceptions_to_rescue += exceptions
end
def handle(*args)
result = nil
- _handled_by = @handlers.find{|h| result = h.call(*args)}
+ _handled_by = @handlers.find{|h| result = try_handler(h, *args)}
result
end
+ attr_reader :rescued_exceptions
+
+ protected
+
+ attr_writer :rescued_exceptions
+ attr_accessor :exceptions_to_rescue
+
+ def try_handler(handler, *args)
+ result = handler.call(*args)
+ rescue *exceptions_to_rescue
+ self.rescued_exceptions << $!
+ result = false
+ end
end
end
diff --git a/test/unit/handler_chain_test.rb b/test/unit/handler_chain_test.rb
index 067f11e..fae0418 100644
--- a/test/unit/handler_chain_test.rb
+++ b/test/unit/handler_chain_test.rb
@@ -23,6 +23,26 @@ class HandlerChainTest < Minitest::Test
assert_equal :result, chain.handle
end
+ def test_raise_exception
+ chain handler_raising, handler_with_result
+ assert_raises RuntimeError do
+ chain.handle
+ end
+ end
+
+ def test_continue_on_exception
+ chain handler_raising, handler_with_result
+ chain.continue_on(RuntimeError)
+ assert_equal :result, chain.handle
+ assert_equal [RuntimeError], chain.rescued_exceptions.map(&:class)
+ end
+
+ def test_continue_on_exception_with_nil
+ chain handler_raising, handler_with_nil
+ chain.continue_on(RuntimeError)
+ assert_nil chain.handle
+ assert_equal [RuntimeError], chain.rescued_exceptions.map(&:class)
+ end
protected
@@ -42,4 +62,7 @@ class HandlerChainTest < Minitest::Test
Proc.new { :result }
end
+ def handler_raising(exception = RuntimeError)
+ Proc.new { raise exception }
+ end
end