diff --git a/lib/ebay_api.rb b/lib/ebay_api.rb index 26afe31..c6d0355 100644 --- a/lib/ebay_api.rb +++ b/lib/ebay_api.rb @@ -84,6 +84,14 @@ class << self end end + response(404) do |_, _, (data, *)| + data = data.to_h + error = data.dig("errors", 0) || {} + code = error["errorId"] + message = error["longMessage"] || error["message"] + raise NotFound.new(code: code), message + end + # https://go.developer.ebay.com/api-call-limits response(429) do |_, _, (data, *)| data = data.to_h diff --git a/lib/ebay_api/exceptions.rb b/lib/ebay_api/exceptions.rb index 477d7e1..5db5b66 100644 --- a/lib/ebay_api/exceptions.rb +++ b/lib/ebay_api/exceptions.rb @@ -12,6 +12,9 @@ def initialize(*args, code: nil, data: nil, **) class InvalidAccessToken < Error; end class AlreadyExists < Error; end + # HTTP 404 from eBay. + class NotFound < Error; end + # HTTP 500 from eBay. May be retried in most cases. class InternalServerError < Error; end diff --git a/spec/error_handling_spec.rb b/spec/error_handling_spec.rb index 4dd5566..18a87ca 100644 --- a/spec/error_handling_spec.rb +++ b/spec/error_handling_spec.rb @@ -43,4 +43,17 @@ class EbayAPI end end end + + context "when offer already deleted" do + let(:response) do + open_fixture_file "resource_not_found" + end + + it do + expect { subject }.to raise_error(EbayAPI::NotFound) do |ex| + expect(ex.code).to eq 25713 + expect(ex.message).to match /This Offer is not available/ + end + end + end end diff --git a/spec/fixtures/resource_not_found b/spec/fixtures/resource_not_found new file mode 100644 index 0000000..46bf472 --- /dev/null +++ b/spec/fixtures/resource_not_found @@ -0,0 +1,5 @@ +HTTP/1.1 404 Not Found +Content-Length: 188 +Content-Type: application/json + +{"errors":[{"errorId":25713,"domain":"API_INVENTORY","subdomain":"Selling","category":"REQUEST","message":"This Offer is not available.","parameters":[{"name":"text1","value":"123456"}]}]} \ No newline at end of file