diff --git a/application/src/main/java/org/togetherjava/tjbot/features/utils/ResponseUtils.java b/application/src/main/java/org/togetherjava/tjbot/features/utils/ResponseUtils.java index c2ab8675fb..dce573b1ec 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/utils/ResponseUtils.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/utils/ResponseUtils.java @@ -1,6 +1,9 @@ package org.togetherjava.tjbot.features.utils; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.databind.ObjectMapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.IOException; import java.io.UncheckedIOException; @@ -12,6 +15,8 @@ * Handle the parsing of json in a http request. */ public class ResponseUtils { + private static final Logger logger = LoggerFactory.getLogger(ResponseUtils.class); + private ResponseUtils() {} /** @@ -29,9 +34,12 @@ public static BodyHandler ofJson(Class type, ObjectMapper mapper) { if (responseInfo.statusCode() == 200 || responseInfo.statusCode() == 204) { return uncheckedParseJson(type, mapper, bytes); } - String errorMessage = tryParseError(bytes, mapper) - .orElse("Request failed with status: " + responseInfo.statusCode()); - throw new UncheckedRequestFailedException(errorMessage, responseInfo.statusCode()); + ErrorAndMessage errorMessage = + tryParseError(bytes, mapper).orElse(new ErrorAndMessage("Bad Request", + "Request failed with status: " + responseInfo.statusCode())); + throw new UncheckedRequestFailedException( + errorMessage.error() + ". " + errorMessage.message(), + responseInfo.statusCode()); }); } @@ -43,10 +51,15 @@ private static T uncheckedParseJson(Class type, ObjectMapper mapper, byte } } - private static Optional tryParseError(byte[] bytes, ObjectMapper mapper) { + @JsonIgnoreProperties(ignoreUnknown = true) + private record ErrorAndMessage(String error, String message) { + } + + private static Optional tryParseError(byte[] bytes, ObjectMapper mapper) { try { - return Optional.ofNullable(mapper.readTree(bytes).get("error").asText()); + return Optional.ofNullable(mapper.readValue(bytes, ErrorAndMessage.class)); } catch (Exception e) { + logger.error("Error parsing json", e); return Optional.empty(); } }