From 14bfb4650dd6b7fd1c9d5e8dfbcbc424f5a97bbc Mon Sep 17 00:00:00 2001 From: Anton Pershakov Date: Tue, 17 Oct 2023 00:15:59 +0100 Subject: [PATCH] Recognize an error when an item can't be accessed. Raise ItemNotAccessible exception instead of general RequestError --- lib/ebay/api.rb | 1 + lib/ebay/response_handler.rb | 21 ++++++++------ .../get_item_with_item_not_accessible.xml | 18 ++++++++++++ .../get_item_with_multiple_failures.xml | 28 +++++++++++++++++++ test/unit/ebay_test.rb | 24 ++++++++++++++++ 5 files changed, 84 insertions(+), 8 deletions(-) create mode 100644 test/fixtures/responses/get_item_with_item_not_accessible.xml create mode 100644 test/fixtures/responses/get_item_with_multiple_failures.xml diff --git a/lib/ebay/api.rb b/lib/ebay/api.rb index ef4ec5e..57b32df 100644 --- a/lib/ebay/api.rb +++ b/lib/ebay/api.rb @@ -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 diff --git a/lib/ebay/response_handler.rb b/lib/ebay/response_handler.rb index 620b252..2e6a9f7 100644 --- a/lib/ebay/response_handler.rb +++ b/lib/ebay/response_handler.rb @@ -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) @@ -16,11 +17,11 @@ 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 @@ -28,8 +29,12 @@ def call 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 diff --git a/test/fixtures/responses/get_item_with_item_not_accessible.xml b/test/fixtures/responses/get_item_with_item_not_accessible.xml new file mode 100644 index 0000000..29ba6d9 --- /dev/null +++ b/test/fixtures/responses/get_item_with_item_not_accessible.xml @@ -0,0 +1,18 @@ + + + 2023-10-16T14:57:46.740Z + Failure + + Item can't be accessed. + This item cannot be accessed because the listing has been deleted or you are not the seller. + 17 + Error + + 123456789 + + RequestError + + 1193 + E1193_CORE_API_19146280_R1 + + diff --git a/test/fixtures/responses/get_item_with_multiple_failures.xml b/test/fixtures/responses/get_item_with_multiple_failures.xml new file mode 100644 index 0000000..3747791 --- /dev/null +++ b/test/fixtures/responses/get_item_with_multiple_failures.xml @@ -0,0 +1,28 @@ + + + 2023-10-16T14:57:46.740Z + Failure + + Item can't be accessed. + This item cannot be accessed because the listing has been deleted or you are not the seller. + 17 + Error + + 123456789 + + RequestError + + + Your application has exceeded usage limit on this call. + Your application has exceeded usage limit on this call, please make call to Developer Analytics API to check your call usage. + 518 + Error + + 123456789 + + RequestError + + 1193 + E1193_CORE_API_19146280_R1 + + diff --git a/test/unit/ebay_test.rb b/test/unit/ebay_test.rb index 2fefd5c..65def5d 100644 --- a/test/unit/ebay_test.rb +++ b/test/unit/ebay_test.rb @@ -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)