Skip to content
Closed
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
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## [Unreleased]

## [0.3.1] - 2026-08-25

- Preserve error response bodies: replace the http gem's raise_error with
flushed_raise_error, which reads the body before raising (fixes #8)

## [0.3.0] - 2026-08-25

- Require http ~> 6.0
Expand All @@ -24,7 +29,8 @@
- OpenAPI-based code generator
- RBS type signatures

[Unreleased]: https://github.com/lineofflight/amazon-ads-ruby/compare/v0.3.0...HEAD
[Unreleased]: https://github.com/lineofflight/amazon-ads-ruby/compare/v0.3.1...HEAD
[0.3.1]: https://github.com/lineofflight/amazon-ads-ruby/compare/v0.3.0...v0.3.1
[0.3.0]: https://github.com/lineofflight/amazon-ads-ruby/compare/v0.2.0...v0.3.0
[0.2.0]: https://github.com/lineofflight/amazon-ads-ruby/compare/v0.1.0...v0.2.0
[0.1.0]: https://github.com/lineofflight/amazon-ads-ruby/releases/tag/v0.1.0
2 changes: 2 additions & 0 deletions lib/amazon_ads.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
loader.inflector.inflect("api" => "API", "lwa" => "LWA")
loader.collapse("#{__dir__}/amazon_ads/apis")
loader.ignore("#{__dir__}/generator")
loader.ignore("#{__dir__}/amazon_ads/flushed_raise_error.rb")
loader.setup
require_relative "amazon_ads/flushed_raise_error"

# Amazon Ads API client for Ruby
module AmazonAds
Expand Down
4 changes: 2 additions & 2 deletions lib/amazon_ads/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ def http
.use(:auto_inflate)

if retries.zero?
client.use(:raise_error)
client.use(:flushed_raise_error)
else
client
.use(raise_error: { ignore: [429] })
.use(flushed_raise_error: { ignore: [429] })
.retriable(tries: retries + 1, retry_statuses: [429])
end
end
Expand Down
32 changes: 32 additions & 0 deletions lib/amazon_ads/flushed_raise_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true
# rbs_inline: enabled

require "http"

module AmazonAds
# Drop-in replacement for the http gem's raise_error feature that reads
# the response body before raising. HTTP::Client#perform closes the
# connection when a feature raises, so with the stock feature the body of
# an error response is lost by the time callers rescue HTTP::StatusError.
class FlushedRaiseError < HTTP::Feature
# Status codes to pass through without raising
attr_reader :ignore #: Array[Integer]

#: (?ignore: Array[Integer]) -> void
def initialize(ignore: [])
super()
@ignore = ignore
end

#: (HTTP::Response) -> HTTP::Response
def wrap_response(response)
return response if response.code < 400
return response if ignore.include?(response.code)

response.flush # memoize the body before Client#perform closes the connection
raise HTTP::StatusError, response
end

HTTP::Options.register_feature(:flushed_raise_error, self)
end
end
2 changes: 1 addition & 1 deletion lib/amazon_ads/lwa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def request

#: () -> HTTP::Client
def http
@http.use(:raise_error)
@http.use(:flushed_raise_error)
end

#: () -> Hash[Symbol, String]
Expand Down
2 changes: 1 addition & 1 deletion lib/amazon_ads/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module AmazonAds
VERSION = "0.3.0"
VERSION = "0.3.1"
end
1 change: 1 addition & 0 deletions test/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ AMAZON_ADS_CLIENT_SECRET=CLIENT_SECRET
AMAZON_ADS_TEST_REFRESH_TOKEN=REFRESH_TOKEN
AMAZON_ADS_TEST_REGION=NA
AMAZON_ADS_TEST_PROFILE_ID=PROFILE_ID
AMAZON_ADS_TEST_EU_PROFILE_ID=PROFILE_ID
14 changes: 14 additions & 0 deletions test/amazon_ads/apis/test_campaigns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,18 @@ def test_query_campaign

assert_predicate(res.status, :success?)
end

def test_query_campaign_rejects_bare_string_filter
api = AmazonAds::Campaigns.new(
region: "EU",
profile_id: ENV.fetch("AMAZON_ADS_TEST_EU_PROFILE_ID", "0"),
access_token:,
)
error = assert_raises(HTTP::StatusError) do
api.query_campaign(ad_product_filter: "SPONSORED_PRODUCTS")
end

assert_equal(400, error.response.status.code)
assert(error.response.body.to_s.length.positive?)
end
end
54 changes: 54 additions & 0 deletions test/amazon_ads/test_flushed_raise_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# frozen_string_literal: true

require "test_helper"
require "socket"

class TestFlushedRaiseError < Minitest::Test
BODY = '{"message":"BAD_REQUEST: adProductFilter must be an object"}'

def setup
# test_helper hooks VCR into WebMock for every test, which intercepts
# HTTP::Client#perform before it ever opens a socket. This test exists
# specifically to exercise that real socket path, so bypass both for
# its duration.
VCR.turn_off!
WebMock.allow_net_connect!

@server = TCPServer.new("127.0.0.1", 0)
@port = @server.addr[1]
@thread = Thread.new do
socket = @server.accept
socket.readpartial(4096)
socket.write(
"HTTP/1.1 400 Bad Request\r\n" \
"Content-Type: application/json\r\n" \
"Content-Length: #{BODY.bytesize}\r\n" \
"Connection: close\r\n" \
"\r\n#{BODY}",
)
socket.close
end
end

def teardown
@thread.kill
@server.close

WebMock.disable_net_connect!
VCR.turn_on!
end

def test_status_error_retains_body
error = assert_raises(HTTP::StatusError) do
HTTP.use(:flushed_raise_error).get("http://127.0.0.1:#{@port}/")
end

assert_equal(BODY, error.response.body.to_s)
end

def test_ignored_codes_pass_through
response = HTTP.use(flushed_raise_error: { ignore: [400] }).get("http://127.0.0.1:#{@port}/")

assert_equal(400, response.status.code)
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading