Skip to content

Commit

Permalink
distinct exception type thrown when best available objects not found (#…
Browse files Browse the repository at this point in the history
…209)

* distinct exception type for when best available seats not found to make it easier to handle this case

* refactoring
  • Loading branch information
bverbeken authored Jan 3, 2025
1 parent 8a0b35a commit 41e22dd
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main/java/seatsio/SeatsioException.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import kong.unirest.HttpRequest;
import kong.unirest.RawResponse;
import seatsio.events.BestAvailableObjectsNotFoundException;
import seatsio.exceptions.RateLimitExceededException;

import java.util.List;
Expand Down Expand Up @@ -38,12 +39,18 @@ public static SeatsioException from(HttpRequest request, RawResponse response, b
SeatsioExceptionTO parsedException = fromJson(responseBody);
if (response.getStatus() == 429) {
return new RateLimitExceededException(parsedException.errors, parsedException.requestId);
} else if (isBestAvailableObjectsNotFound(parsedException)) {
return new BestAvailableObjectsNotFoundException(parsedException.errors, parsedException.requestId);
}
return new SeatsioException(parsedException.errors, parsedException.requestId);
}
return new SeatsioException(request, response);
}

private static boolean isBestAvailableObjectsNotFound(SeatsioExceptionTO parsedException) {
return parsedException.errors.stream().anyMatch(e -> "BEST_AVAILABLE_OBJECTS_NOT_FOUND".equals(e.getCode()));
}

private static String exceptionMessage(List<ApiError> errors) {
return errors.stream().map(ApiError::getMessage).collect(joining(", "));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package seatsio.events;

import seatsio.ApiError;
import seatsio.SeatsioException;

import java.util.List;

public class BestAvailableObjectsNotFoundException extends SeatsioException {

public BestAvailableObjectsNotFoundException(List<ApiError> errors, String requestId) {
super(errors, requestId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import org.junit.jupiter.api.Test;
import seatsio.SeatsioClientTest;
import seatsio.SeatsioException;

import java.util.List;
import java.util.Map;
import java.util.Set;

import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Fail.fail;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class ChangeBestAvailableObjectStatusTest extends SeatsioClientTest {

Expand Down Expand Up @@ -195,4 +198,30 @@ public void accessibleSeats() {
assertThat(bestAvailableResult.nextToEachOther).isTrue();
assertThat(bestAvailableResult.objects).containsOnly("A-6", "A-7", "A-8");
}

@Test
public void notFoundThrowsBestAvailableObjectsNotFoundException() {
String chartKey = createTestChart();
Event event = client.events.create(chartKey);

try {
client.events.changeObjectStatus(event.key, new BestAvailableParams.Builder().withNumber(3000).build(), "foo");
fail("expected BestAvailableObjectsNotFoundException");
} catch(BestAvailableObjectsNotFoundException e) {
assertThat(e).isInstanceOf(BestAvailableObjectsNotFoundException.class);
assertThat(e.getMessage()).isEqualTo("Best available objects not found");
}
}

@Test
public void notFoundThrowsNormalSeatsioExceptionIfEventNotFound() {
String chartKey = createTestChart();
client.events.create(chartKey);

try {
client.events.changeObjectStatus("unknownEvent", new BestAvailableParams.Builder().withNumber(3).build(), "foo");
} catch(SeatsioException e) {
assertThat(e).isNotInstanceOf(BestAvailableObjectsNotFoundException.class);
}
}
}

0 comments on commit 41e22dd

Please sign in to comment.