Skip to content

Commit

Permalink
Instead of throwing a generic SeatsioException (which is thrown in ma…
Browse files Browse the repository at this point in the history
…ny 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.
  • Loading branch information
bverbeken committed Dec 23, 2024
1 parent e078f20 commit af8507e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
3 changes: 3 additions & 0 deletions lib/seatsio/exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ class SeatsioException < StandardError
class RateLimitExceededException < SeatsioException
end

class BestAvailableObjectsNotFoundException < SeatsioException
end

class NoMorePagesException < SeatsioException
end

Expand Down
29 changes: 24 additions & 5 deletions lib/seatsio/httpClient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
5 changes: 5 additions & 0 deletions test/custom_assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 21 additions & 0 deletions test/events/change_best_available_object_status_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit af8507e

Please sign in to comment.