|
| 1 | +import { CodeBlock } from '@/components/code-block'; |
| 2 | +import { EditPageLink } from '@/components/edit-page-link'; |
| 3 | +import { HeadingWithAnchor } from '@/components/heading-with-anchor'; |
| 4 | + |
| 5 | +export default function PostmarkErrorHandlingPage() { |
| 6 | + return ( |
| 7 | + <div className="space-y-6"> |
| 8 | + <HeadingWithAnchor id="postmark-error-handling" level={1}> |
| 9 | + Handling Postmark Errors |
| 10 | + </HeadingWithAnchor> |
| 11 | + <p className="text-lg text-neutral-600 dark:text-neutral-300"> |
| 12 | + A guide to handling Postmark bounce errors and sending custom notifications using LogStruct. |
| 13 | + </p> |
| 14 | + |
| 15 | + <HeadingWithAnchor id="error-handling-methods" level={2}> |
| 16 | + Error Handling Methods |
| 17 | + </HeadingWithAnchor> |
| 18 | + <p className="text-neutral-600 dark:text-neutral-300 mb-4"> |
| 19 | + LogStruct provides three error handling methods you can use in <code>rescue_from</code>{' '} |
| 20 | + handlers: |
| 21 | + </p> |
| 22 | + <ul className="list-disc list-inside space-y-2 text-neutral-600 dark:text-neutral-300 mb-4"> |
| 23 | + <li> |
| 24 | + <code>log_and_ignore_error(error)</code> - Logs the error but doesn't report to Sentry or |
| 25 | + reraise |
| 26 | + </li> |
| 27 | + <li> |
| 28 | + <code>log_and_report_error(error)</code> - Logs the error, reports to Sentry, but doesn't |
| 29 | + reraise |
| 30 | + </li> |
| 31 | + <li> |
| 32 | + <code>log_and_reraise_error(error)</code> - Logs the error, reports to Sentry, and |
| 33 | + reraises (for retry) |
| 34 | + </li> |
| 35 | + </ul> |
| 36 | + |
| 37 | + <HeadingWithAnchor id="example-postmark-bounces" level={2}> |
| 38 | + Example: Handling Postmark Bounce Errors |
| 39 | + </HeadingWithAnchor> |
| 40 | + <p className="text-neutral-600 dark:text-neutral-300 mb-4"> |
| 41 | + When using Postmark, you may want to handle bounce errors (inactive recipients, invalid |
| 42 | + emails) differently than other errors: |
| 43 | + </p> |
| 44 | + <CodeBlock language="ruby">{`# config/initializers/action_mailer_postmark_errors.rb |
| 45 | +Rails.application.config.after_initialize do |
| 46 | + ActiveSupport.on_load(:action_mailer) do |
| 47 | + if defined?(Postmark) |
| 48 | + # Postmark bounce errors - log but don't reraise (can't be retried) |
| 49 | + rescue_from Postmark::InactiveRecipientError, with: :log_and_ignore_error |
| 50 | + rescue_from Postmark::InvalidEmailRequestError, with: :log_and_ignore_error |
| 51 | + end |
| 52 | + end |
| 53 | +end`}</CodeBlock> |
| 54 | + |
| 55 | + <HeadingWithAnchor id="custom-notifications" level={2}> |
| 56 | + Custom Notifications (e.g., Slack) |
| 57 | + </HeadingWithAnchor> |
| 58 | + <p className="text-neutral-600 dark:text-neutral-300 mb-4"> |
| 59 | + LogStruct logs structured error data but doesn't send notifications directly. To send custom |
| 60 | + notifications (like Slack alerts), subscribe to LogStruct logs: |
| 61 | + </p> |
| 62 | + <CodeBlock language="ruby">{`# config/initializers/mailer_notifications.rb |
| 63 | +Rails.application.config.after_initialize do |
| 64 | + # Subscribe to LogStruct logs |
| 65 | + ActiveSupport::Notifications.subscribe('log.logstruct') do |_name, _start, _finish, _id, payload| |
| 66 | + log = payload[:log] |
| 67 | +
|
| 68 | + # Send Slack notification for Postmark errors |
| 69 | + if log.is_a?(LogStruct::Log::ActionMailer::Error) && |
| 70 | + [Postmark::InactiveRecipientError, Postmark::InvalidEmailRequestError].include?(log.error_class) |
| 71 | +
|
| 72 | + InternalSlackNotificationJob.perform_async( |
| 73 | + channel: '#alerts', |
| 74 | + text: "Email delivery error: #{log.message}" |
| 75 | + ) |
| 76 | + end |
| 77 | + end |
| 78 | +end`}</CodeBlock> |
| 79 | + |
| 80 | + <HeadingWithAnchor id="error-log-structure" level={2}> |
| 81 | + Error Log Structure |
| 82 | + </HeadingWithAnchor> |
| 83 | + <p className="text-neutral-600 dark:text-neutral-300 mb-4"> |
| 84 | + When a mailer error occurs, LogStruct creates a{' '} |
| 85 | + <code>LogStruct::Log::ActionMailer::Error</code> with: |
| 86 | + </p> |
| 87 | + <ul className="list-disc list-inside space-y-2 text-neutral-600 dark:text-neutral-300 mb-4"> |
| 88 | + <li> |
| 89 | + <code>error_class</code> - The exception class (e.g.,{' '} |
| 90 | + <code>Postmark::InactiveRecipientError</code>) |
| 91 | + </li> |
| 92 | + <li> |
| 93 | + <code>message</code> - Formatted message including context and the actual exception |
| 94 | + message |
| 95 | + </li> |
| 96 | + <li> |
| 97 | + <code>backtrace</code> - Stack trace |
| 98 | + </li> |
| 99 | + <li> |
| 100 | + <code>mailer_class</code> - The mailer class name |
| 101 | + </li> |
| 102 | + <li> |
| 103 | + <code>mailer_action</code> - The mailer action/method name |
| 104 | + </li> |
| 105 | + <li> |
| 106 | + <code>to</code>, <code>from</code>, <code>subject</code> - Email metadata |
| 107 | + </li> |
| 108 | + <li> |
| 109 | + <code>attachment_count</code> - Number of attachments |
| 110 | + </li> |
| 111 | + <li> |
| 112 | + <code>additional_data</code> - Custom context (account_id, user_id, etc.) |
| 113 | + </li> |
| 114 | + </ul> |
| 115 | + |
| 116 | + <HeadingWithAnchor id="error-handling-flow" level={2}> |
| 117 | + Error Handling Flow |
| 118 | + </HeadingWithAnchor> |
| 119 | + <ol className="list-decimal list-inside space-y-2 text-neutral-600 dark:text-neutral-300 mb-4"> |
| 120 | + <li>Email delivery fails with an exception</li> |
| 121 | + <li> |
| 122 | + Rails <code>rescue_from</code> catches the exception |
| 123 | + </li> |
| 124 | + <li> |
| 125 | + LogStruct method is called (e.g., <code>log_and_ignore_error</code>) |
| 126 | + </li> |
| 127 | + <li>LogStruct creates structured error log</li> |
| 128 | + <li>Your notification subscriber receives the log</li> |
| 129 | + <li>You send custom notifications based on error type</li> |
| 130 | + </ol> |
| 131 | + |
| 132 | + <EditPageLink path="docs/app/docs/guides/postmark-error-handling/page.tsx" /> |
| 133 | + </div> |
| 134 | + ); |
| 135 | +} |
0 commit comments