Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ The SDK has two independent subsystems sharing a common error hierarchy:
Error
├── HttpRequestError (has status_code, response_body)
│ ├── ValidationError (422, has error_type)
│ └── ClientError (400)
│ ├── ClientError (400)
│ ├── AuthenticationError (401/403)
│ └── RateLimitError (429, has retry_after)
├── ConnectionError (has original_exception)
├── TimeoutError
└── WebhookVerificationError
├── InvalidSignatureError
Expand Down
4 changes: 4 additions & 0 deletions lib/lettermint/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ def with_error_handling
handle_response(response)
rescue Faraday::TimeoutError, Timeout::Error
raise Lettermint::TimeoutError, "Request timeout after #{@connection.options.timeout}s"
rescue Faraday::SSLError => e
raise Lettermint::ConnectionError.new(message: "SSL error: #{e.message}", original_exception: e)
rescue Faraday::ConnectionFailed => e
raise Lettermint::ConnectionError.new(message: e.message, original_exception: e)
rescue Faraday::ParsingError => e
raise Lettermint::Error, "Failed to parse API response: #{e.message}"
Comment thread
delano marked this conversation as resolved.
end

def handle_response(response)
Expand Down
32 changes: 32 additions & 0 deletions spec/lettermint/http_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,38 @@
expect(e.original_exception).to be_a(Faraday::ConnectionFailed)
}
end

it 'raises HttpRequestError on non-JSON error response body' do
stub_request(:post, "#{base_url}/send")
.to_return(
status: 500,
body: 'Internal Server Error',
headers: { 'Content-Type' => 'text/plain' }
)

expect { client.post(path: '/send', data: {}) }
.to raise_error(Lettermint::HttpRequestError) { |e|
expect(e.status_code).to eq(500)
expect(e.response_body).to be_nil
expect(e.message).to eq('HTTP 500')
}
end

it 'raises ConnectionError on SSL failure' do
stub_request(:post, "#{base_url}/send").to_raise(Faraday::SSLError.new('certificate verify failed'))

expect { client.post(path: '/send', data: {}) }
.to raise_error(Lettermint::ConnectionError, /SSL error:.*certificate verify failed/) { |e|
expect(e.original_exception).to be_a(Faraday::SSLError)
}
end

it 'raises Error on JSON parsing failure' do
stub_request(:post, "#{base_url}/send").to_raise(Faraday::ParsingError.new('unexpected token'))

expect { client.post(path: '/send', data: {}) }
.to raise_error(Lettermint::Error, /unexpected token|parsing/i)
end
end

describe 'custom base URL' do
Expand Down
Loading