Skip to content

Commit

Permalink
Add exception for 429 Too Many Requests errors
Browse files Browse the repository at this point in the history
When rate limits are hit a generic `CreateSend::ClientError` is raised
with no explanatory details, which makes implementing a backoff / retry
mechanism difficult. If the same request is made manually, we may see a
response like:

```
{
  "Code" => 429,
  "Message" => "Subscriber was added too many times too quickly, try again later."
}
```

Which allow us to differentiate between rate limiting errors and other
client errors. This adds a new `CreateSend::TooManyRequests` exception
to be raised when the API responds with such a 429 so we can do so in
code.

`CreateSend::TooManyRequests` inherits from `ClientError`, so any
current code that rescues `ClientError` should be unaffected.
  • Loading branch information
ags authored and dentarg committed Mar 1, 2024
1 parent d1af1f0 commit 08bb286
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
4 changes: 4 additions & 0 deletions lib/createsend/createsend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class BadRequest < CreateSendError; end
class Unauthorized < CreateSendError; end
# Raised for HTTP response code of 404
class NotFound < ClientError; end
# Raised for HTTP response code of 429
class TooManyRequests < ClientError; end

# Raised for HTTP response code of 401, specifically when an OAuth token
# in invalid (Code: 120, Message: 'Invalid OAuth Token')
Expand Down Expand Up @@ -279,6 +281,8 @@ def handle_response(response) # :nodoc:
end
when 404
raise NotFound.new
when 429
raise TooManyRequests.new
when 400...500
raise ClientError.new
when 500...600
Expand Down
11 changes: 6 additions & 5 deletions test/createsend_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,12 @@ class CreateSendTest < Test::Unit::TestCase
@template = CreateSend::Template.new @auth, '98y2e98y289dh89h938389'
end

{ ["400", "Bad Request"] => CreateSend::BadRequest,
["401", "Unauthorized"] => CreateSend::Unauthorized,
["404", "Not Found"] => CreateSend::NotFound,
["418", "I'm a teapot"] => CreateSend::ClientError,
["500", "Server Error"] => CreateSend::ServerError
{ ["400", "Bad Request"] => CreateSend::BadRequest,
["401", "Unauthorized"] => CreateSend::Unauthorized,
["404", "Not Found"] => CreateSend::NotFound,
["418", "I'm a teapot"] => CreateSend::ClientError,
["429", "Too many requests"] => CreateSend::TooManyRequests,
["500", "Server Error"] => CreateSend::ServerError
}.each do |status, exception|
context "#{status.first}, a get" do
should "raise a #{exception.name} error" do
Expand Down

0 comments on commit 08bb286

Please sign in to comment.