Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/main/java/rest/koios/client/backend/api/base/BaseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ protected <T> Result<T> processResponseGetOne(Call<List<T>> call) throws ApiExce
}
} else {
String errorBody = Objects.requireNonNull(response.errorBody()).string();
return (Result<T>) Result.builder().successful(false).response(errorBody).code(response.code()).error(parseError(errorBody)).build();
return (Result<T>) Result.builder().successful(false).response(errorBody).code(response.code()).error(errorFor(errorBody, response.code())).build();
}
} catch (IOException e) {
throw new ApiException(e.getMessage(), e);
Expand Down Expand Up @@ -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.
* <p>
* 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 <T> Result<T> badRequestResult(String responseText) {
return (Result<T>) Result.builder().successful(false).response(responseText).code(400).synthetic(true).build();
}
Expand All @@ -192,7 +219,7 @@ protected <T> Result<T> processResponse(Call<?> call) throws ApiException {
return (Result<T>) Result.builder().successful(true).response(response.toString()).value(response.body()).code(response.code()).build();
} else {
String errorBody = Objects.requireNonNull(response.errorBody()).string();
return (Result<T>) Result.builder().successful(false).response(errorBody).code(response.code()).error(parseError(errorBody)).build();
return (Result<T>) Result.builder().successful(false).response(errorBody).code(response.code()).error(errorFor(errorBody, response.code())).build();
}
} catch (IOException e) {
throw new ApiException(e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Tip> 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<Tip> 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
Expand Down
Loading