From af8507e5ad62272fc439017bb0a8604ff5ad3036 Mon Sep 17 00:00:00 2001 From: Ben Verbeken Date: Mon, 23 Dec 2024 21:08:16 +0100 Subject: [PATCH] Instead of throwing a generic SeatsioException (which is thrown in many cases, eg no focal point, event not found, etc), we now throw a BestAvailableObjectsNotFoundException (subclass of SeatsioException) to make it easier to handle this common specific case. --- lib/seatsio/exception.rb | 3 ++ lib/seatsio/httpClient.rb | 29 +++++++++++++++---- test/custom_assertions.rb | 5 ++++ ...hange_best_available_object_status_test.rb | 21 ++++++++++++++ 4 files changed, 53 insertions(+), 5 deletions(-) diff --git a/lib/seatsio/exception.rb b/lib/seatsio/exception.rb index ecad43b..18ff328 100644 --- a/lib/seatsio/exception.rb +++ b/lib/seatsio/exception.rb @@ -6,6 +6,9 @@ class SeatsioException < StandardError class RateLimitExceededException < SeatsioException end + class BestAvailableObjectsNotFoundException < SeatsioException + end + class NoMorePagesException < SeatsioException end diff --git a/lib/seatsio/httpClient.rb b/lib/seatsio/httpClient.rb index 7faf343..9ab2673 100644 --- a/lib/seatsio/httpClient.rb +++ b/lib/seatsio/httpClient.rb @@ -49,11 +49,7 @@ def execute(*args) rescue RestClient::NotFound => e raise Exception::NotFoundException.new(e.response) rescue RestClient::ExceptionWithResponse => e - if e.response.code == 429 - raise Exception::RateLimitExceededException.new(e.response) - else - raise Exception::SeatsioException.new(e.response) - end + handle_exception(e.response) rescue RestClient::Exceptions::Timeout raise Exception::SeatsioException.new("Timeout ERROR") rescue SocketError @@ -94,5 +90,28 @@ def post(endpoint, payload = {}) def delete(endpoint, payload = {}) execute(:delete, endpoint, payload) end + + private + + def handle_exception(response) + content_type = response.headers[:content_type] + if content_type&.include?("application/json") + parsed_exception = JSON.parse(response.body, symbolize_names: true) + if response.code == 429 + raise Exception::RateLimitExceededException.new(response) + elsif best_available_objects_not_found?(parsed_exception[:errors]) + raise Exception::BestAvailableObjectsNotFoundException.new(response) + else + raise Exception::SeatsioException.new(response) + end + else + raise Exception::SeatsioException.new(response) + end + end + + def best_available_objects_not_found?(errors) + errors.any? { |error| error[:code] == "BEST_AVAILABLE_OBJECTS_NOT_FOUND" } + end + end end diff --git a/test/custom_assertions.rb b/test/custom_assertions.rb index 3f7f22c..2a7bda6 100644 --- a/test/custom_assertions.rb +++ b/test/custom_assertions.rb @@ -30,4 +30,9 @@ def assert_not_equal(expected, actual) def assert_empty(actual) assert_equal([], actual) end + + def assert_not_instance_of(expected_class, actual) + refute_instance_of(expected_class, actual) + end + end diff --git a/test/events/change_best_available_object_status_test.rb b/test/events/change_best_available_object_status_test.rb index 767082c..6ec7aef 100644 --- a/test/events/change_best_available_object_status_test.rb +++ b/test/events/change_best_available_object_status_test.rb @@ -213,4 +213,25 @@ def test_accessible_seats assert_equal(true, result.next_to_each_other) assert_equal(%w(A-6 A-7 A-8), result.objects) end + + def test_specific_exception_type_when_best_available_result_not_found + chart_key = create_test_chart + event = @seatsio.events.create chart_key: chart_key + + begin + @seatsio.events.change_best_available_object_status(event.key, 3000, 'myStatus') + raise "Should have failed" + rescue Seatsio::Exception::BestAvailableObjectsNotFoundException => e + assert_equal("BEST_AVAILABLE_OBJECTS_NOT_FOUND", JSON.parse(e.message)['errors'].first['code']) + end + end + + def test_general_exception_type_when_eg_event_not_found + begin + @seatsio.events.change_best_available_object_status("unexisting_event", 3000, 'myStatus') + raise "Should have failed" + rescue Seatsio::Exception::SeatsioException => e + assert_not_instance_of(Seatsio::Exception::BestAvailableObjectsNotFoundException, e) + end + end end