Skip to content

Commit

Permalink
improved error handling
Browse files Browse the repository at this point in the history
Mailtrap sending client used to raise `server error` on 403.
  • Loading branch information
i7an committed Oct 4, 2023
1 parent 001adc5 commit 129fd10
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 5 deletions.
15 changes: 15 additions & 0 deletions lib/mailtrap/sending.rb
Expand Up @@ -17,6 +17,21 @@ def initialize(messages)
end
end

# AuthorizationError is raised when invalid token is used.
class AuthorizationError < Error; end

# MailSizeError is raised when mail is too large.
class MailSizeError < Error; end

# RateLimitError is raised when client performing too many requests.
class RateLimitError < Error; end

# RejectionError is raised when server refuses to process the request. Use
# error message to debug the problem.
#
# *Some* possible reasons:
# * Account is banned
# * Domain is not verified
class RejectionError < Error; end
end
end
20 changes: 15 additions & 5 deletions lib/mailtrap/sending/client.rb
Expand Up @@ -44,15 +44,25 @@ def post_request(path, body)
end

def handle_response(response)
case response.code
when '200'
case response
when Net::HTTPOK
json_response(response.body)
when '400'
when Net::HTTPBadRequest
raise Mailtrap::Sending::Error, json_response(response.body)[:errors]
when '401'
when Net::HTTPUnauthorized
raise Mailtrap::Sending::AuthorizationError, json_response(response.body)[:errors]
else
when Net::HTTPForbidden
raise Mailtrap::Sending::RejectionError, json_response(response.body)[:errors]
when Net::HTTPPayloadTooLarge
raise Mailtrap::Sending::MailSizeError, ['message too large']
when Net::HTTPTooManyRequests
raise Mailtrap::Sending::RateLimitError, ['too many requests']
when Net::HTTPClientError
raise Mailtrap::Sending::Error, ['client error']
when Net::HTTPServerError
raise Mailtrap::Sending::Error, ['server error']
else
raise Mailtrap::Sending::Error, ["unexpected status code=#{response.code}"]
end
end

Expand Down
67 changes: 67 additions & 0 deletions spec/mailtrap/sending/client_spec.rb
Expand Up @@ -108,4 +108,71 @@
end
end
end

describe 'errors' do
let(:mail) do
Mailtrap::Mail::Base.new(
from: { email: 'from@example.com' },
to: [{ email: 'to@example.com' }],
subject: 'Test',
text: 'Test'
)
end

subject { client.send(mail) }

def stub_api_send(status, body=nil)
stub = stub_request(:post, /\/api\/send/).to_return(status: status, body: body)
yield
expect(stub).to have_been_requested
end

it 'handles 400' do
stub_api_send 400, '{"errors":["error"]}' do
expect { subject }.to raise_error(Mailtrap::Sending::Error)
end
end

it 'handles 401' do
stub_api_send 401, '{"errors":["Unauthorized"]}' do
expect { subject }.to raise_error(Mailtrap::Sending::AuthorizationError)
end
end

it 'handles 403' do
stub_api_send 403, '{"errors":["Account is banned"]}' do
expect { subject }.to raise_error(Mailtrap::Sending::RejectionError)
end
end

it 'handles 413' do
stub_api_send 413 do
expect { subject }.to raise_error(Mailtrap::Sending::MailSizeError)
end
end

it 'handles 429' do
stub_api_send 429 do
expect { subject }.to raise_error(Mailtrap::Sending::RateLimitError)
end
end

it 'handles generic client errors' do
stub_api_send 418, '🫖' do
expect { subject }.to raise_error(Mailtrap::Sending::Error, 'client error')
end
end

it 'handles server errors' do
stub_api_send 504, '🫖' do
expect { subject }.to raise_error(Mailtrap::Sending::Error, 'server error')
end
end

it 'handles unexpected response status code' do
stub_api_send 307 do
expect { subject }.to raise_error(Mailtrap::Sending::Error, 'unexpected status code=307')
end
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Expand Up @@ -3,6 +3,7 @@
require 'mail'
require 'mailtrap'
require 'rspec/its'
require 'webmock/rspec'
require 'vcr'

VCR.configure do |config|
Expand Down

0 comments on commit 129fd10

Please sign in to comment.