Skip to content

Error Handling

Jens Balvig edited this page Jan 12, 2024 · 2 revisions

For the gem version 1.11.0 and above, use the following template to handle the errors you care about:

client = Postmark::ApiClient.new('...')

begin
  client.deliver(
    from: '...',
    to: '...',
    subject: 'Hello from Postmark',
    message_stream: 'outbound'
  )
rescue Postmark::InvalidApiKeyError => error
  # Authentication error
  # TODO: Make sure your API token is correct
  puts error
rescue Postmark::TimeoutError => error
  # Network timeout, auto-retried :max_retries times
  # TODO: Save message locally, try again once the network issues are resolved
  # Consider increasing `http_open_timeout` and `http_read_timeout`.
  puts error
rescue Postmark::InternalServerError => error
  # Postmark server error, auto-retried :max_retries times
  # TODO: Save message locally, try again later.
  puts error
rescue Postmark::HttpClientError => error
  # Corrupted response from Postmark, auto-retried :max_retries times
  # TODO: Save message locally, try again later.
  puts error
rescue Postmark::InactiveRecipientError => error
  # You tried to send to one or more recipients marked as inactive in
  # Postmark
  # TODO: Mark listed recipients as inactive in your local db or reactivate
  # using the Bounces API
  puts "Inactive recipients: #{error.recipients.join(', ')}"
  puts error
rescue Postmark::ApiInputError => error
  # Postmark rejected your request as invalid
  # TODO: Look up the error code and resolve the problem in your app
  # List of supported error codes:
  # https://postmarkapp.com/developer/api/overview#error-codes
  puts "#{error.error_code} #{error.message}"
  puts error
rescue Postmark::Error => error
  # All other Postmark errors
  # TODO: Log and review as needed
  puts error
rescue Errno::EINVAL, Errno::ECONNRESET, Errno::ECONNREFUSED,
  EOFError, Net::ProtocolError, SocketError => error
  # Standard Ruby network errors, auto-retried :max_reties times
  # TODO: Save message locally, resolve network issues, try again.
  puts error
end