1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activesupport/test/rescuable_test.rb
Jeremy Daer e35b98e6f5
Action Mailer: Declarative exception handling with rescue_from.
Follows the same pattern as controllers and jobs. Exceptions raised in
delivery jobs (enqueued by `#deliver_later`) are also delegated to the
mailer's rescue_from handlers, so you can handle the DeserializationError
raised by delivery jobs:

```ruby
class MyMailer < ApplicationMailer
  rescue_from ActiveJob::DeserializationError do
    …
  end
```

ActiveSupport::Rescuable polish:
* Add the `rescue_with_handler` class method so exceptions may be
  handled at the class level without requiring an instance.
* Rationalize `exception.cause` handling. If no handler matches the
  exception, fall back to the handler that matches its cause.
* Handle exceptions raised elsewhere. Pass `object: …` to execute
  the `rescue_from` handler (e.g. a method call or a block to
  instance_exec) against a different object. Defaults to `self`.
2016-05-15 18:44:16 -07:00

143 lines
3 KiB
Ruby

require 'abstract_unit'
class WraithAttack < StandardError
end
class MadRonon < StandardError
end
class CoolError < StandardError
end
module WeirdError
def self.===(other)
Exception === other && other.respond_to?(:weird?)
end
end
class Stargate
# Nest this so the 'NuclearExplosion' handler needs a lexical const_get
# to find it.
class NuclearExplosion < StandardError; end
attr_accessor :result
include ActiveSupport::Rescuable
rescue_from WraithAttack, :with => :sos_first
rescue_from WraithAttack, :with => :sos
rescue_from 'NuclearExplosion' do
@result = 'alldead'
end
rescue_from MadRonon do |e|
@result = e.message
end
rescue_from WeirdError do
@result = 'weird'
end
def dispatch(method)
send(method)
rescue Exception => e
rescue_with_handler(e)
end
def attack
raise WraithAttack
end
def nuke
raise NuclearExplosion
end
def ronanize
raise MadRonon.new("dex")
end
def fall_back_to_cause
# This exception is the cause and has a handler.
ronanize
rescue
# This is the exception we'll handle that doesn't have a cause.
raise 'unhandled RuntimeError with a handleable cause'
end
def weird
StandardError.new.tap do |exc|
def exc.weird?
true
end
raise exc
end
end
def sos
@result = 'killed'
end
def sos_first
@result = 'sos_first'
end
end
class CoolStargate < Stargate
attr_accessor :result
include ActiveSupport::Rescuable
rescue_from CoolError, :with => :sos_cool_error
def sos_cool_error
@result = 'sos_cool_error'
end
end
class RescuableTest < ActiveSupport::TestCase
def setup
@stargate = Stargate.new
@cool_stargate = CoolStargate.new
end
def test_rescue_from_with_method
@stargate.dispatch :attack
assert_equal 'killed', @stargate.result
end
def test_rescue_from_with_block
@stargate.dispatch :nuke
assert_equal 'alldead', @stargate.result
end
def test_rescue_from_with_block_with_args
@stargate.dispatch :ronanize
assert_equal 'dex', @stargate.result
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
expected = ["WraithAttack", "WraithAttack", "NuclearExplosion", "MadRonon", "WeirdError"]
result = @stargate.send(:rescue_handlers).collect(&:first)
assert_equal expected, result
end
def test_children_should_inherit_rescue_definitions_from_parents_and_child_rescue_should_be_appended
expected = ["WraithAttack", "WraithAttack", "NuclearExplosion", "MadRonon", "WeirdError", "CoolError"]
result = @cool_stargate.send(:rescue_handlers).collect(&:first)
assert_equal expected, result
end
def test_rescue_falls_back_to_exception_cause
@stargate.dispatch :fall_back_to_cause
assert_equal 'unhandled RuntimeError with a handleable cause', @stargate.result
end
end