aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorGenadi Samokovarov <gsamokovarov@gmail.com>2018-12-29 17:27:33 +0200
committerGenadi Samokovarov <gsamokovarov@gmail.com>2019-04-19 14:15:22 +0900
commita3110fe20bc418034332bd01165df7fe6f20258e (patch)
tree2bbddbe477f6078d1d75f9614c40953dc4ad09a6 /activesupport
parent54df392bc51dcf424f07ccc10157a5969256ba73 (diff)
downloadrails-a3110fe20bc418034332bd01165df7fe6f20258e.tar.gz
rails-a3110fe20bc418034332bd01165df7fe6f20258e.tar.bz2
rails-a3110fe20bc418034332bd01165df7fe6f20258e.zip
Drop the ambiguous `ActiveSupport::ActionableError#===` check
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/actionable_error.rb23
-rw-r--r--activesupport/test/actionable_error_test.rb15
2 files changed, 25 insertions, 13 deletions
diff --git a/activesupport/lib/active_support/actionable_error.rb b/activesupport/lib/active_support/actionable_error.rb
index aeee2177aa..be4972759b 100644
--- a/activesupport/lib/active_support/actionable_error.rb
+++ b/activesupport/lib/active_support/actionable_error.rb
@@ -15,20 +15,25 @@ module ActiveSupport
NonActionable = Class.new(StandardError)
- included do
- class_attribute :_actions, default: Hash.new do |_, label|
- raise NonActionable, "Cannot find action \"#{label}\" for #{self}"
- end
+ NoActions = Hash.new do |_, label| # :nodoc:
+ raise NonActionable, "Cannot find action \"#{label}\" for #{self}"
end
- def self.===(other) # :nodoc:
- super || Module === other && other.ancestors.include?(self)
+ included do
+ class_attribute :_actions, default: NoActions.dup
end
def self.actions(error) # :nodoc:
- error = error.constantize if String === error
- raise NonActionable, "#{error.name} is non-actionable" unless self === error
- error._actions
+ case error
+ when String
+ actions(error.constantize)
+ when ActionableError, -> it { Class === it && it < ActionableError }
+ error._actions
+ when Exception
+ NoActions
+ else
+ raise NonActionable, "#{error} is non-actionable"
+ end
end
def self.dispatch(error, label) # :nodoc:
diff --git a/activesupport/test/actionable_error_test.rb b/activesupport/test/actionable_error_test.rb
index 66ba94e0dd..80614b4700 100644
--- a/activesupport/test/actionable_error_test.rb
+++ b/activesupport/test/actionable_error_test.rb
@@ -4,8 +4,7 @@ require "abstract_unit"
require "active_support/actionable_error"
class ActionableErrorTest < ActiveSupport::TestCase
- class NonActionableError < StandardError
- end
+ NonActionableError = Class.new(StandardError)
class DispatchableError < StandardError
include ActiveSupport::ActionableError
@@ -22,15 +21,23 @@ class ActionableErrorTest < ActiveSupport::TestCase
end
end
- test "can get all action of an actionable error" do
+ test "lists all action of an actionable error" do
assert_equal ["Flip 1", "Flip 2"], ActiveSupport::ActionableError.actions(DispatchableError).keys
assert_equal ["Flip 1", "Flip 2"], ActiveSupport::ActionableError.actions(DispatchableError.new).keys
end
- test "cannot get actions from non-actionable errors" do
+ test "raises an error when trying to get actions from non-actionable error classes" do
assert_raises ActiveSupport::ActionableError::NonActionable do
ActiveSupport::ActionableError.actions(NonActionableError)
end
+
+ assert_raises ActiveSupport::ActionableError::NonActionable do
+ ActiveSupport::ActionableError.actions(NonActionableError.name)
+ end
+ end
+
+ test "returns no actions from non-actionable exception instances" do
+ assert ActiveSupport::ActionableError.actions(Exception.new).empty?
end
test "dispatches actions from class and a label" do