1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/actionmailer/lib/action_mailer/rescuable.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

27 lines
618 B
Ruby

module ActionMailer #:nodoc:
# Provides `rescue_from` for mailers. Wraps mailer action processing,
# mail job processing, and mail delivery.
module Rescuable
extend ActiveSupport::Concern
include ActiveSupport::Rescuable
class_methods do
def handle_exception(exception) #:nodoc:
rescue_with_handler(exception) || raise(exception)
end
end
def handle_exceptions #:nodoc:
yield
rescue => exception
rescue_with_handler(exception) || raise
end
private
def process(*)
handle_exceptions do
super
end
end
end
end