Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recognize an error when an item can't be accessed #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions lib/ebay/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def initialize(errors)
end

class RequestLimitExceeded < RequestError; end
class ItemNotAccessible < RequestError; end

# == Overview
# Api is the main proxy class responsible for instantiating and invoking
Expand Down
21 changes: 13 additions & 8 deletions lib/ebay/response_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ class ResponseHandler
# Error codes explained:
# https://developer.ebay.com/devzone/xml/docs/Reference/ebay/Errors/errormessages.htm
ERROR_CODES = [
(REQUEST_LIMIT_EXCEEDED = '518').freeze
(REQUEST_LIMIT_EXCEEDED = '518').freeze,
(ITEM_NOT_ACCESSIBLE = '17').freeze
].freeze

def initialize(response)
Expand All @@ -16,20 +17,24 @@ def initialize(response)
def call
case response.ack
when Ebay::Types::AckCode::Failure, Ebay::Types::AckCode::PartialFailure
if request_limit_exceeded?(response.errors)
raise RequestLimitExceeded.new(response.errors)
else
raise RequestError.new(response.errors)
end
# The check for request limit error has highest priority
raise RequestLimitExceeded.new(response.errors) if request_limit_exceeded?
raise ItemNotAccessible.new(response.errors) if item_not_accessible?

raise RequestError.new(response.errors)
end

response
end

private

def request_limit_exceeded?(errors)
errors.any? { |error| error.error_code.to_s == REQUEST_LIMIT_EXCEEDED }
def request_limit_exceeded?
response.errors.any? { |error| error.error_code.to_s == REQUEST_LIMIT_EXCEEDED }
end

def item_not_accessible?
response.errors.any? { |error| error.error_code.to_s == ITEM_NOT_ACCESSIBLE }
end
end
end
18 changes: 18 additions & 0 deletions test/fixtures/responses/get_item_with_item_not_accessible.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version='1.0' encoding='UTF-8'?>
<GetItemResponse xmlns='urn:ebay:apis:eBLBaseComponents'>
<Timestamp>2023-10-16T14:57:46.740Z</Timestamp>
<Ack>Failure</Ack>
<Errors>
<ShortMessage>Item can&apos;t be accessed.</ShortMessage>
<LongMessage>This item cannot be accessed because the listing has been deleted or you are not the seller.</LongMessage>
<ErrorCode>17</ErrorCode>
<SeverityCode>Error</SeverityCode>
<ErrorParameters ParamID='0'>
<Value>123456789</Value>
</ErrorParameters>
<ErrorClassification>RequestError</ErrorClassification>
</Errors>
<Version>1193</Version>
<Build>E1193_CORE_API_19146280_R1</Build>
<Item/>
</GetItemResponse>
28 changes: 28 additions & 0 deletions test/fixtures/responses/get_item_with_multiple_failures.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version='1.0' encoding='UTF-8'?>
<GetItemResponse xmlns='urn:ebay:apis:eBLBaseComponents'>
<Timestamp>2023-10-16T14:57:46.740Z</Timestamp>
<Ack>Failure</Ack>
<Errors>
<ShortMessage>Item can&apos;t be accessed.</ShortMessage>
<LongMessage>This item cannot be accessed because the listing has been deleted or you are not the seller.</LongMessage>
<ErrorCode>17</ErrorCode>
<SeverityCode>Error</SeverityCode>
<ErrorParameters ParamID='0'>
<Value>123456789</Value>
</ErrorParameters>
<ErrorClassification>RequestError</ErrorClassification>
</Errors>
<Errors>
<ShortMessage>Your application has exceeded usage limit on this call.</ShortMessage>
<LongMessage>Your application has exceeded usage limit on this call, please make call to Developer Analytics API to check your call usage.</LongMessage>
<ErrorCode>518</ErrorCode>
<SeverityCode>Error</SeverityCode>
<ErrorParameters ParamID='0'>
<Value>123456789</Value>
</ErrorParameters>
<ErrorClassification>RequestError</ErrorClassification>
</Errors>
<Version>1193</Version>
<Build>E1193_CORE_API_19146280_R1</Build>
<Item/>
</GetItemResponse>
24 changes: 24 additions & 0 deletions test/unit/ebay_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,30 @@ def test_raise_on_error_with_request_limit_exceeded_failure
end
end

def test_raise_on_error_with_item_not_accessible
Ebay::HttpMock.respond_with responses(:get_item_with_item_not_accessible)
begin
@ebay.get_item(item_id: '123456789')
rescue Ebay::RequestError => exception
assert_equal 1, exception.errors.size
error = exception.errors.first
assert_equal 'Item can\'t be accessed.', error.short_message
assert_equal 'This item cannot be accessed because the listing has been deleted or you are not the seller.', error.long_message
assert_equal ErrorClassificationCode::RequestError, error.error_classification
assert_equal exception.class, Ebay::ItemNotAccessible
end
end

def test_priority_of_request_limit_exceeded_failure
Ebay::HttpMock.respond_with responses(:get_item_with_multiple_failures)
begin
@ebay.get_item(item_id: '123456789')
rescue Ebay::RequestError => exception
assert_equal 2, exception.errors.size
assert_equal exception.class, Ebay::RequestLimitExceeded
end
end

def test_force_encoding_to_default
response_in_ascii = load_response(:local_lang_chars).force_encoding('ASCII-8BIT')
Ebay::HttpMock.respond_with parse_response(response_in_ascii)
Expand Down