From 8853180f8ec2693bdd607a1b764d22f906eb77f8 Mon Sep 17 00:00:00 2001 From: dudiedri Date: Sat, 12 Sep 2026 22:41:42 +0300 Subject: [PATCH] Give 413 responses an actionable error Step 4 of #6, and the last item on it. Koios enforces a request body size limit on the bulk endpoints. Exceeding it returns 413 with a plain-text body: Payload too large, body length was 18022. Please ensure your request body size is below 5120 bytes Because that is text rather than JSON, the parser added in #210 correctly leaves Result.error null, so callers got a bare status and had to know on their own that the fix is to batch. 413 now produces a KoiosError carrying the server's message verbatim plus a hint to split the request. Nothing is invented about the response itself: the status is real, so synthetic stays false, and the message is the server's own words. Only the remedy is ours, because Koios does not supply one. Co-Authored-By: Claude Opus 5 --- .../client/backend/api/base/BaseService.java | 31 +++++++++++++++++-- .../base/BaseServiceErrorHandlingTest.java | 27 +++++++++++++++- 2 files changed, 55 insertions(+), 3 deletions(-) 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 362e3dd..7552f7b 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 @@ -145,7 +145,7 @@ protected Result processResponseGetOne(Call> call) throws ApiExce } } else { String errorBody = Objects.requireNonNull(response.errorBody()).string(); - return (Result) Result.builder().successful(false).response(errorBody).code(response.code()).error(parseError(errorBody)).build(); + return (Result) Result.builder().successful(false).response(errorBody).code(response.code()).error(errorFor(errorBody, response.code())).build(); } } catch (IOException e) { throw new ApiException(e.getMessage(), e); @@ -173,6 +173,33 @@ private static KoiosError parseError(String body) { } } + /** + * Builds the error for a failing response, adding guidance for statuses that Koios reports as + * plain text rather than JSON. + *

+ * A 413 means the request body exceeded the instance's size limit, which Koios enforces on the + * bulk endpoints such as {@code /pool_info} and {@code /drep_info}. The server reports it as + * plain text, so there is nothing to parse; the message is kept verbatim and a hint is added, + * since the fix is always to send fewer ids per call. + * + * @param body raw response body + * @param httpCode HTTP status of the response + * @return parsed or constructed error, or null when the body carries nothing usable + */ + private static KoiosError errorFor(String body, int httpCode) { + KoiosError parsed = parseError(body); + if (parsed != null) { + return parsed; + } + if (httpCode == 413) { + String message = (body == null || body.trim().isEmpty()) + ? "Request body too large" : body.trim(); + return new KoiosError(String.valueOf(httpCode), message, null, + "Split the request into smaller batches and call the endpoint once per batch."); + } + return null; + } + protected Result badRequestResult(String responseText) { return (Result) Result.builder().successful(false).response(responseText).code(400).synthetic(true).build(); } @@ -192,7 +219,7 @@ protected Result processResponse(Call call) throws ApiException { return (Result) Result.builder().successful(true).response(response.toString()).value(response.body()).code(response.code()).build(); } else { String errorBody = Objects.requireNonNull(response.errorBody()).string(); - return (Result) Result.builder().successful(false).response(errorBody).code(response.code()).error(parseError(errorBody)).build(); + return (Result) Result.builder().successful(false).response(errorBody).code(response.code()).error(errorFor(errorBody, response.code())).build(); } } catch (IOException e) { throw new ApiException(e.getMessage(), e); 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 2d3e5a0..904b28c 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 @@ -136,12 +136,37 @@ void internalServerErrorIsSurfacedTest() throws ApiException { @Test void payloadTooLargeIsSurfacedTest() throws ApiException { - server.enqueue(new MockResponse().setResponseCode(413).setBody("Request body too large")); + // verbatim from a live mainnet /pool_info call with 300 ids + String koiosText = "Payload too large, body length was 18022. " + + "Please ensure your request body size is below 5120 bytes"; + server.enqueue(new MockResponse().setResponseCode(413).setBody(koiosText)); Result result = backendService.getNetworkService().getChainTip(); Assertions.assertFalse(result.isSuccessful()); Assertions.assertEquals(413, result.getCode()); + Assertions.assertFalse(result.isSynthetic(), "413 is a real server response"); + // Koios reports this as plain text, so there is nothing to parse; the client supplies the + // remedy, which is always to batch + KoiosError error = result.getError(); + Assertions.assertNotNull(error); + Assertions.assertEquals("413", error.getCode()); + Assertions.assertEquals(koiosText, error.getMessage()); + Assertions.assertNotNull(error.getHint()); + Assertions.assertTrue(error.getHint().contains("batches"), error.getHint()); + } + + @Test + void payloadTooLargeWithEmptyBodyStillCarriesGuidanceTest() throws ApiException { + server.enqueue(new MockResponse().setResponseCode(413)); + + Result result = backendService.getNetworkService().getChainTip(); + + KoiosError error = result.getError(); + Assertions.assertNotNull(error); + Assertions.assertEquals("413", error.getCode()); + Assertions.assertEquals("Request body too large", error.getMessage()); + Assertions.assertNotNull(error.getHint()); } @Test