diff options
author | Kasper Timm Hansen <kaspth@gmail.com> | 2019-04-19 15:38:57 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-19 15:38:57 +0200 |
commit | 10da0a27512e108a5cde3eeba774b01c15f6c43a (patch) | |
tree | 95f93befe2e9723cade192bc6af06412bf6f6f6f /activesupport/lib | |
parent | 3c8ea319d0fa4cd1e707a294ce1be4ffc8cd78b9 (diff) | |
parent | feaaa7576a0774471e36dc59730a886f623712e6 (diff) | |
download | rails-10da0a27512e108a5cde3eeba774b01c15f6c43a.tar.gz rails-10da0a27512e108a5cde3eeba774b01c15f6c43a.tar.bz2 rails-10da0a27512e108a5cde3eeba774b01c15f6c43a.zip |
Merge pull request #34788 from gsamokovarov/actionable-errors
Introduce Actionable Errors
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support.rb | 1 | ||||
-rw-r--r-- | activesupport/lib/active_support/actionable_error.rb | 48 |
2 files changed, 49 insertions, 0 deletions
diff --git a/activesupport/lib/active_support.rb b/activesupport/lib/active_support.rb index 5589c71281..9e242ddeaa 100644 --- a/activesupport/lib/active_support.rb +++ b/activesupport/lib/active_support.rb @@ -34,6 +34,7 @@ module ActiveSupport extend ActiveSupport::Autoload autoload :Concern + autoload :ActionableError autoload :CurrentAttributes autoload :Dependencies autoload :DescendantsTracker diff --git a/activesupport/lib/active_support/actionable_error.rb b/activesupport/lib/active_support/actionable_error.rb new file mode 100644 index 0000000000..7db14cd178 --- /dev/null +++ b/activesupport/lib/active_support/actionable_error.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +module ActiveSupport + # Actionable errors let's you define actions to resolve an error. + # + # To make an error actionable, include the <tt>ActiveSupport::ActionableError</tt> + # module and invoke the +action+ class macro to define the action. An action + # needs a name and a block to execute. + module ActionableError + extend Concern + + class NonActionable < StandardError; end + + included do + class_attribute :_actions, default: {} + end + + def self.actions(error) # :nodoc: + case error + when ActionableError, -> it { Class === it && it < ActionableError } + error._actions + else + {} + end + end + + def self.dispatch(error, name) # :nodoc: + actions(error).fetch(name).call + rescue KeyError + raise NonActionable, "Cannot find action \"#{name}\"" + end + + module ClassMethods + # Defines an action that can resolve the error. + # + # class PendingMigrationError < MigrationError + # include ActiveSupport::ActionableError + # + # action "Run pending migrations" do + # ActiveRecord::Tasks::DatabaseTasks.migrate + # end + # end + def action(name, &block) + _actions[name] = block + end + end + end +end |