diff --git a/src/main/java/rest/koios/client/backend/api/base/BaseService.java b/src/main/java/rest/koios/client/backend/api/base/BaseService.java index 379e2f3..362e3dd 100644 --- a/src/main/java/rest/koios/client/backend/api/base/BaseService.java +++ b/src/main/java/rest/koios/client/backend/api/base/BaseService.java @@ -139,9 +139,9 @@ protected Result processResponseGetOne(Call> call) throws ApiExce if (response.body() != null && !response.body().isEmpty()) { return (Result) Result.builder().successful(true).response(response.toString()).value(response.body().get(0)).code(response.code()).build(); } else if (response.body() != null) { - return (Result) Result.builder().successful(false).response("Response Body is Empty").code(404).build(); + return (Result) Result.builder().successful(false).response("Response Body is Empty").code(404).synthetic(true).build(); } else { - return (Result) Result.builder().successful(false).response("Response Body is Invalid").code(500).build(); + return (Result) Result.builder().successful(false).response("Response Body is Invalid").code(500).synthetic(true).build(); } } else { String errorBody = Objects.requireNonNull(response.errorBody()).string(); @@ -174,7 +174,7 @@ private static KoiosError parseError(String body) { } protected Result badRequestResult(String responseText) { - return (Result) Result.builder().successful(false).response(responseText).code(400).build(); + return (Result) Result.builder().successful(false).response(responseText).code(400).synthetic(true).build(); } /** diff --git a/src/main/java/rest/koios/client/backend/api/base/Result.java b/src/main/java/rest/koios/client/backend/api/base/Result.java index d90afc2..7907699 100644 --- a/src/main/java/rest/koios/client/backend/api/base/Result.java +++ b/src/main/java/rest/koios/client/backend/api/base/Result.java @@ -23,5 +23,19 @@ public class Result { * Null on success, and null when the body was not JSON of that shape. */ KoiosError error; + + /** + * Whether this result was constructed by the client rather than reported by the server. + *

+ * The client returns a failed result in a few cases where no HTTP error occurred at all - a + * request rejected by local validation before it was sent, or a 200 response whose body was + * empty or missing where a single object was expected. Those results still carry a + * {@code code} so that callers branching on it keep working, but the code was chosen by the + * client and never came from Koios. + *

+ * False for every result that reflects an actual HTTP response, successful or not. Check this + * before treating {@code code} as a server status. + */ + boolean synthetic; } diff --git a/src/test/java/rest/koios/client/backend/api/base/BaseServiceErrorHandlingTest.java b/src/test/java/rest/koios/client/backend/api/base/BaseServiceErrorHandlingTest.java index 4f69cb2..2d3e5a0 100644 --- a/src/test/java/rest/koios/client/backend/api/base/BaseServiceErrorHandlingTest.java +++ b/src/test/java/rest/koios/client/backend/api/base/BaseServiceErrorHandlingTest.java @@ -7,11 +7,14 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import rest.koios.client.backend.api.base.exception.ApiException; +import rest.koios.client.backend.api.asset.model.AssetInformation; import rest.koios.client.backend.api.network.model.Tip; +import rest.koios.client.backend.factory.options.Options; import rest.koios.client.backend.factory.BackendFactory; import rest.koios.client.backend.factory.BackendService; import java.io.IOException; +import java.util.List; /** * Error handling tests that run against a local {@link MockWebServer} instead of a live Koios @@ -49,6 +52,19 @@ void successfulResponseIsReturnedTest() throws ApiException { Assertions.assertEquals("Conway", result.getValue().getEra()); } + @Test + void locallyRejectedRequestIsSyntheticTest() throws ApiException { + // rejected by client-side validation, so no request is ever sent + Result> result = + backendService.getAssetService().getAssetInformationBulk(null, Options.EMPTY); + + Assertions.assertFalse(result.isSuccessful()); + Assertions.assertEquals(400, result.getCode()); + Assertions.assertTrue(result.isSynthetic(), "no request was sent, so the 400 is the client's"); + Assertions.assertNull(result.getError()); + Assertions.assertEquals(0, server.getRequestCount(), "nothing should have been sent"); + } + @Test void successCarriesNoErrorTest() throws ApiException { server.enqueue(new MockResponse() @@ -60,6 +76,7 @@ void successCarriesNoErrorTest() throws ApiException { Assertions.assertTrue(result.isSuccessful()); Assertions.assertNull(result.getError()); + Assertions.assertFalse(result.isSynthetic()); } @Test @@ -91,6 +108,7 @@ void serverErrorBodyIsSurfacedTest() throws ApiException { Assertions.assertFalse(result.isSuccessful()); Assertions.assertEquals(400, result.getCode()); + Assertions.assertFalse(result.isSynthetic(), "a real server 400 is not synthetic"); Assertions.assertEquals(postgrestError, result.getResponse()); Assertions.assertNull(result.getValue()); // the same body, parsed @@ -110,6 +128,7 @@ void internalServerErrorIsSurfacedTest() throws ApiException { Assertions.assertFalse(result.isSuccessful()); Assertions.assertEquals(500, result.getCode()); + Assertions.assertFalse(result.isSynthetic(), "a real server 500 is not synthetic"); Assertions.assertEquals("upstream failure", result.getResponse()); // a plain-text body is not a structured error; the raw text is still available Assertions.assertNull(result.getError()); @@ -135,7 +154,10 @@ void emptyResponseBodyReportsNotFoundTest() throws ApiException { Result result = backendService.getNetworkService().getChainTip(); Assertions.assertFalse(result.isSuccessful()); + // the server returned 200; this 404 is the client's, not Koios's Assertions.assertEquals(404, result.getCode()); + Assertions.assertTrue(result.isSynthetic(), "an empty body yields a client-generated code"); + Assertions.assertNull(result.getError()); Assertions.assertEquals("Response Body is Empty", result.getResponse()); }