diff options
author | Jeremy Daer <jeremydaer@gmail.com> | 2016-05-13 17:43:48 -0700 |
---|---|---|
committer | Jeremy Daer <jeremydaer@gmail.com> | 2016-05-15 18:44:16 -0700 |
commit | e35b98e6f5c54330245645f2ed40d56c74538902 (patch) | |
tree | 272bba30020bde27d177d2cf6227dfa8bb7a525f /activesupport/test | |
parent | 6810847f495cd282c2659be738ac8271ecdec12f (diff) | |
download | rails-e35b98e6f5c54330245645f2ed40d56c74538902.tar.gz rails-e35b98e6f5c54330245645f2ed40d56c74538902.tar.bz2 rails-e35b98e6f5c54330245645f2ed40d56c74538902.zip |
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`.
Diffstat (limited to 'activesupport/test')
-rw-r--r-- | activesupport/test/rescuable_test.rb | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/activesupport/test/rescuable_test.rb b/activesupport/test/rescuable_test.rb index bd43ad0797..e42e6d2973 100644 --- a/activesupport/test/rescuable_test.rb +++ b/activesupport/test/rescuable_test.rb @@ -3,9 +3,6 @@ require 'abstract_unit' class WraithAttack < StandardError end -class NuclearExplosion < StandardError -end - class MadRonon < StandardError end @@ -19,6 +16,10 @@ module WeirdError 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 @@ -57,6 +58,14 @@ class Stargate 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? @@ -74,7 +83,6 @@ class Stargate def sos_first @result = 'sos_first' end - end class CoolStargate < Stargate @@ -127,4 +135,9 @@ class RescuableTest < ActiveSupport::TestCase 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 |