From b4ffd303e95a5f633340ba39b02217006042b586 Mon Sep 17 00:00:00 2001 From: dudiedri Date: Sat, 12 Sep 2026 22:04:07 +0300 Subject: [PATCH 1/2] Expose a typed error on Result Step 2 of #6. Koios is served by PostgREST, which reports failures as a JSON object: {"code":"42703","details":null,"hint":null, "message":"column record.no_such_column does not exist"} Callers only ever saw that as an opaque String on Result.response and had to parse it themselves. Adds KoiosError (code, message, details, hint) and a Result.error field populated when a failing response carries a body of that shape. The existing response String is untouched, so no current caller changes behaviour - Result.error is simply null where it used to be unavailable. Bodies that are not JSON of that shape, such as a plain-text 500, leave error null rather than guessing. Only the two sites where the body genuinely comes from the server parse it; the synthetic failures built locally do not pretend to carry a server error. Verified against live mainnet: a real 42703 parses to KoiosError(code=42703, message=column record.no_such_column does not exist, details=null, hint=null). Co-Authored-By: Claude Opus 5 --- .../client/backend/api/base/BaseService.java | 35 ++++++++++++-- .../client/backend/api/base/KoiosError.java | 46 +++++++++++++++++++ .../koios/client/backend/api/base/Result.java | 6 +++ .../base/BaseServiceErrorHandlingTest.java | 38 +++++++++++++++ 4 files changed, 121 insertions(+), 4 deletions(-) create mode 100644 src/main/java/rest/koios/client/backend/api/base/KoiosError.java 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 fb7b62b..379e2f3 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 @@ -35,6 +35,9 @@ @Getter public class BaseService { + private static final ObjectMapper ERROR_MAPPER = new ObjectMapper() + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + private final Retrofit retrofit; private int retriesCount = 5; private boolean retryOnTimeout = true; @@ -141,13 +144,35 @@ protected Result processResponseGetOne(Call> call) throws ApiExce return (Result) Result.builder().successful(false).response("Response Body is Invalid").code(500).build(); } } else { - return (Result) Result.builder().successful(false).response(Objects.requireNonNull(response.errorBody()).string()).code(response.code()).build(); + String errorBody = Objects.requireNonNull(response.errorBody()).string(); + return (Result) Result.builder().successful(false).response(errorBody).code(response.code()).error(parseError(errorBody)).build(); } } catch (IOException e) { throw new ApiException(e.getMessage(), e); } } + /** + * Parses a JSON error body returned by Koios into a {@link KoiosError}. + * + * @param body raw response body, which may be plain text rather than JSON + * @return parsed error, or null if the body is blank, is not JSON, or carries none of the + * expected fields + */ + private static KoiosError parseError(String body) { + if (body == null || body.trim().isEmpty()) { + return null; + } + try { + KoiosError error = ERROR_MAPPER.readValue(body, KoiosError.class); + boolean empty = error.getCode() == null && error.getMessage() == null + && error.getDetails() == null && error.getHint() == null; + return empty ? null : error; + } catch (IOException e) { + return null; + } + } + protected Result badRequestResult(String responseText) { return (Result) Result.builder().successful(false).response(responseText).code(400).build(); } @@ -163,10 +188,12 @@ protected Result badRequestResult(String responseText) { protected Result processResponse(Call call) throws ApiException { try { Response response = (Response) execute(call); - if (response.isSuccessful()) + if (response.isSuccessful()) { return (Result) Result.builder().successful(true).response(response.toString()).value(response.body()).code(response.code()).build(); - else - return (Result) Result.builder().successful(false).response(Objects.requireNonNull(response.errorBody()).string()).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(); + } } catch (IOException e) { throw new ApiException(e.getMessage(), e); } diff --git a/src/main/java/rest/koios/client/backend/api/base/KoiosError.java b/src/main/java/rest/koios/client/backend/api/base/KoiosError.java new file mode 100644 index 0000000..2077f39 --- /dev/null +++ b/src/main/java/rest/koios/client/backend/api/base/KoiosError.java @@ -0,0 +1,46 @@ +package rest.koios.client.backend.api.base; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import lombok.*; + +/** + * Structured error returned by a Koios instance. + *

+ * Koios is served by PostgREST, which reports failures as a JSON object rather than plain text: + *

+ * {"code":"42703","details":null,"hint":null,"message":"column pgrst_call.ticker does not exist"}
+ * 
+ * When a failing response carries such a body it is parsed into this object and exposed via + * {@link Result#getError()}. Responses whose body is not JSON of this shape leave it null; the raw + * body is always available from {@link Result#getResponse()} either way. + */ +@Getter +@Setter +@ToString +@NoArgsConstructor +@AllArgsConstructor +@EqualsAndHashCode +@JsonIgnoreProperties(ignoreUnknown = true) +public class KoiosError { + + /** + * Error code as reported by the server, e.g. a PostgreSQL SQLSTATE such as "42703" + * (null if not applicable) + */ + private String code; + + /** + * Human readable description of the failure (null if not applicable) + */ + private String message; + + /** + * Additional detail about the failure (null if not applicable) + */ + private String details; + + /** + * Suggested way to resolve the failure (null if not applicable) + */ + private String hint; +} 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 4fa8533..d90afc2 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 @@ -17,5 +17,11 @@ public class Result { String response; int code; T value; + + /** + * Structured error, when the failing response carried a JSON error body. + * Null on success, and null when the body was not JSON of that shape. + */ + KoiosError error; } 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 bc03179..4f69cb2 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 @@ -49,6 +49,35 @@ void successfulResponseIsReturnedTest() throws ApiException { Assertions.assertEquals("Conway", result.getValue().getEra()); } + @Test + void successCarriesNoErrorTest() throws ApiException { + server.enqueue(new MockResponse() + .setResponseCode(200) + .setHeader("Content-Type", "application/json") + .setBody("[{\"hash\":\"abc\",\"epoch_no\":655,\"era\":\"Conway\"}]")); + + Result result = backendService.getNetworkService().getChainTip(); + + Assertions.assertTrue(result.isSuccessful()); + Assertions.assertNull(result.getError()); + } + + @Test + void partialErrorBodyIsParsedTest() throws ApiException { + server.enqueue(new MockResponse() + .setResponseCode(400) + .setHeader("Content-Type", "application/json") + .setBody("{\"message\":\"request body too large\",\"hint\":\"send fewer ids\"}")); + + Result result = backendService.getNetworkService().getChainTip(); + + KoiosError error = result.getError(); + Assertions.assertNotNull(error); + Assertions.assertEquals("request body too large", error.getMessage()); + Assertions.assertEquals("send fewer ids", error.getHint()); + Assertions.assertNull(error.getCode()); + } + @Test void serverErrorBodyIsSurfacedTest() throws ApiException { String postgrestError = "{\"code\":\"42703\",\"details\":null,\"hint\":null," + @@ -64,6 +93,13 @@ void serverErrorBodyIsSurfacedTest() throws ApiException { Assertions.assertEquals(400, result.getCode()); Assertions.assertEquals(postgrestError, result.getResponse()); Assertions.assertNull(result.getValue()); + // the same body, parsed + KoiosError error = result.getError(); + Assertions.assertNotNull(error); + Assertions.assertEquals("42703", error.getCode()); + Assertions.assertEquals("column pgrst_call.ticker does not exist", error.getMessage()); + Assertions.assertNull(error.getDetails()); + Assertions.assertNull(error.getHint()); } @Test @@ -75,6 +111,8 @@ void internalServerErrorIsSurfacedTest() throws ApiException { Assertions.assertFalse(result.isSuccessful()); Assertions.assertEquals(500, result.getCode()); 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()); } @Test From 385e4f6c46bdb00a6f92bc375f34dd77171d149e Mon Sep 17 00:00:00 2001 From: dudiedri Date: Sat, 12 Sep 2026 22:16:20 +0300 Subject: [PATCH 2/2] Fix javadoc references to Lombok-generated getters KoiosError referenced Result#getError() and Result#getResponse(), which Lombok generates at compile time and javadoc therefore cannot resolve: KoiosError.java:14: error: reference not found That failed the CodeQL build, which runs the full javadoc. References the fields instead. Co-Authored-By: Claude Opus 5 --- .../java/rest/koios/client/backend/api/base/KoiosError.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/rest/koios/client/backend/api/base/KoiosError.java b/src/main/java/rest/koios/client/backend/api/base/KoiosError.java index 2077f39..78033f9 100644 --- a/src/main/java/rest/koios/client/backend/api/base/KoiosError.java +++ b/src/main/java/rest/koios/client/backend/api/base/KoiosError.java @@ -10,9 +10,9 @@ *
  * {"code":"42703","details":null,"hint":null,"message":"column pgrst_call.ticker does not exist"}
  * 
- * When a failing response carries such a body it is parsed into this object and exposed via - * {@link Result#getError()}. Responses whose body is not JSON of this shape leave it null; the raw - * body is always available from {@link Result#getResponse()} either way. + * When a failing response carries such a body it is parsed into this object and exposed as the + * {@code error} field of {@link Result}. Responses whose body is not JSON of this shape leave it + * null; the raw body is always available from the {@code response} field either way. */ @Getter @Setter