amelioration(Dolist::ApiSender): lève une Dolist::IgnorableError afin de l'inscrire dans l'historique des EmailEvent

This commit is contained in:
Martin 2023-04-18 16:15:47 +02:00 committed by mfo
parent 8fa2bbb67d
commit 534ce34f87
3 changed files with 25 additions and 9 deletions

View file

@ -154,6 +154,18 @@ class Dolist::API
post(url, body)["FieldList"].find { _1['ID'] == 72 }['Value']
end
def ignorable_error?(response, mail)
error_code = response&.dig("ResponseStatus", "ErrorCode")
invalid_contact_status = if ignorable_api_error_code?(error_code)
fetch_contact_status(mail.to.first)
else
nil
end
[error_code, invalid_contact_status]
end
private
def ignorable_api_error_code?(api_error_code)
IGNORABLE_API_ERROR_CODE.include?(api_error_code)
end
@ -162,8 +174,6 @@ class Dolist::API
IGNORABLE_CONTACT_STATUSES.include?(contact_status)
end
private
def format_url(base)
format(base, account_id: account_id)
end

View file

@ -19,12 +19,18 @@ module MailerMonitoringConcern
end
end
rescue_from Dolist::IgnorableError, with: :log_delivery_error
def log_and_raise_delivery_error(exception)
EmailEvent.create_from_message!(message, status: "dispatch_error")
log_delivery_error(exception)
Sentry.capture_exception(exception, extra: { to: message.to, subject: message.subject })
# re-raise another error so job will retry later
raise MailDeliveryError.new(exception)
end
def log_delivery_error(exception)
EmailEvent.create_from_message!(message, status: "dispatch_error")
end
end
end

View file

@ -1,5 +1,8 @@
ActiveSupport.on_load(:action_mailer) do
module Dolist
class IgnorableError < StandardError
end
class SMTP < ::Mail::SMTP
def deliver!(mail)
mail.from(ENV['DOLIST_NO_REPLY_EMAIL'])
@ -20,14 +23,11 @@ ActiveSupport.on_load(:action_mailer) do
if response&.dig("Result")
mail.message_id = response.dig("Result")
else
error_code = response&.dig("ResponseStatus", "ErrorCode")
_, invalid_contact_status = client.ignorable_error?(response, mail)
contact_status = if client.ignorable_api_error_code?(error_code)
client.fetch_contact_status(mail.to.first)
if invalid_contact_status
raise Dolist::IgnorableError.new("DoList delivery error. contact unreachable: #{invalid_contact_status}")
else
nil
end
if !client.ignorable_contact_status?(contact_status)
fail "DoList delivery error. Body: #{response}"
end
end