提交 dd96e8e3 编写于 作者: G Genadi Samokovarov

Add class level case operator support for error dispatching in Rescuable

上级 6f4fd2c6
* Added support for error dispatcher classes in ActiveSupport::Rescuable. Now it acts closer to Ruby's rescue.
class BaseController < ApplicationController
module ErrorDispatcher
def self.===(other)
Exception === other && other.respond_to?(:status)
end
end
rescue_from ErrorDispatcher do |error|
render status: error.status, json: { error: error.to_s }
end
end
*Genadi Samokovarov*
* Added `#verified` and `#valid_message?` methods to `ActiveSupport::MessageVerifier` * Added `#verified` and `#valid_message?` methods to `ActiveSupport::MessageVerifier`
Previously, the only way to decode a message with `ActiveSupport::MessageVerifier` was to use `#verify`, which would raise an exception on invalid messages. Now `#verified` can also be used, which returns `nil` on messages that cannot be decoded. Previously, the only way to decode a message with `ActiveSupport::MessageVerifier` was to use `#verify`, which would raise an exception on invalid messages. Now `#verified` can also be used, which returns `nil` on messages that cannot be decoded.
......
...@@ -60,7 +60,7 @@ def rescue_from(*klasses, &block) ...@@ -60,7 +60,7 @@ def rescue_from(*klasses, &block)
end end
klasses.each do |klass| klasses.each do |klass|
key = if klass.is_a?(Class) && klass <= Exception key = if klass.is_a?(Module) && klass.respond_to?(:===)
klass.name klass.name
elsif klass.is_a?(String) elsif klass.is_a?(String)
klass klass
...@@ -101,7 +101,7 @@ def handler_for_rescue(exception) ...@@ -101,7 +101,7 @@ def handler_for_rescue(exception)
# itself when rescue_from CONSTANT is executed. # itself when rescue_from CONSTANT is executed.
klass = self.class.const_get(klass_name) rescue nil klass = self.class.const_get(klass_name) rescue nil
klass ||= klass_name.constantize rescue nil klass ||= klass_name.constantize rescue nil
exception.is_a?(klass) if klass klass === exception if klass
end end
case rescuer case rescuer
......
...@@ -12,6 +12,12 @@ class MadRonon < StandardError ...@@ -12,6 +12,12 @@ class MadRonon < StandardError
class CoolError < StandardError class CoolError < StandardError
end end
module WeirdError
def self.===(other)
Exception === other && other.respond_to?(:weird?)
end
end
class Stargate class Stargate
attr_accessor :result attr_accessor :result
...@@ -29,6 +35,10 @@ class Stargate ...@@ -29,6 +35,10 @@ class Stargate
@result = e.message @result = e.message
end end
rescue_from WeirdError do
@result = 'weird'
end
def dispatch(method) def dispatch(method)
send(method) send(method)
rescue Exception => e rescue Exception => e
...@@ -47,6 +57,16 @@ def ronanize ...@@ -47,6 +57,16 @@ def ronanize
raise MadRonon.new("dex") raise MadRonon.new("dex")
end end
def weird
StandardError.new.tap do |exc|
def exc.weird?
true
end
raise exc
end
end
def sos def sos
@result = 'killed' @result = 'killed'
end end
...@@ -91,14 +111,19 @@ def test_rescue_from_with_block_with_args ...@@ -91,14 +111,19 @@ def test_rescue_from_with_block_with_args
assert_equal 'dex', @stargate.result assert_equal 'dex', @stargate.result
end end
def test_rescue_from_error_dispatchers_with_case_operator
@stargate.dispatch :weird
assert_equal 'weird', @stargate.result
end
def test_rescues_defined_later_are_added_at_end_of_the_rescue_handlers_array def test_rescues_defined_later_are_added_at_end_of_the_rescue_handlers_array
expected = ["WraithAttack", "WraithAttack", "NuclearExplosion", "MadRonon"] expected = ["WraithAttack", "WraithAttack", "NuclearExplosion", "MadRonon", "WeirdError"]
result = @stargate.send(:rescue_handlers).collect(&:first) result = @stargate.send(:rescue_handlers).collect(&:first)
assert_equal expected, result assert_equal expected, result
end end
def test_children_should_inherit_rescue_definitions_from_parents_and_child_rescue_should_be_appended def test_children_should_inherit_rescue_definitions_from_parents_and_child_rescue_should_be_appended
expected = ["WraithAttack", "WraithAttack", "NuclearExplosion", "MadRonon", "CoolError"] expected = ["WraithAttack", "WraithAttack", "NuclearExplosion", "MadRonon", "WeirdError", "CoolError"]
result = @cool_stargate.send(:rescue_handlers).collect(&:first) result = @cool_stargate.send(:rescue_handlers).collect(&:first)
assert_equal expected, result assert_equal expected, result
end end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册