Skip to content

Commit

Permalink
Merge pull request rails#49083 from 1st8/actionmailer-log-subscriber-…
Browse files Browse the repository at this point in the history
…log-errors

Log delivery errors in ActionMailer::LogSubscriber
  • Loading branch information
rafaelfranca authored Sep 27, 2023
2 parents 54f3048 + e7009b1 commit d6d47fb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
5 changes: 3 additions & 2 deletions actionmailer/lib/action_mailer/log_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ class LogSubscriber < ActiveSupport::LogSubscriber
# An email was delivered.
def deliver(event)
info do
perform_deliveries = event.payload[:perform_deliveries]
if perform_deliveries
if exception = event.payload[:exception_object]
"Failed delivery of mail #{event.payload[:message_id]} error_class=#{exception.class} error_message=#{exception.message.inspect}"
elsif event.payload[:perform_deliveries]
"Delivered mail #{event.payload[:message_id]} (#{event.duration.round(1)}ms)"
else
"Skipped delivery of mail #{event.payload[:message_id]} as `perform_deliveries` is false"
Expand Down
22 changes: 19 additions & 3 deletions actionmailer/test/log_subscriber_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ def setup
ActionMailer::LogSubscriber.attach_to :action_mailer
end

class TestMailer < ActionMailer::Base
def receive(mail)
# Do nothing
class BogusDelivery
def initialize(*)
end

def deliver!(mail)
raise "failed"
end
end

Expand Down Expand Up @@ -50,4 +53,17 @@ def test_deliver_message_when_perform_deliveries_is_false
ensure
BaseMailer.deliveries.clear
end

def test_deliver_message_when_exception_happened
previous_delivery_method = BaseMailer.delivery_method
BaseMailer.delivery_method = BogusDelivery

assert_raises(RuntimeError) { BaseMailer.welcome(message_id: "123@abc").deliver_now }
wait

assert_equal(1, @logger.logged(:info).size)
assert_equal('Failed delivery of mail 123@abc error_class=RuntimeError error_message="failed"', @logger.logged(:info).first)
ensure
BaseMailer.delivery_method = previous_delivery_method
end
end

0 comments on commit d6d47fb

Please sign in to comment.