Skip to content

Commit 12928bc

Browse files
committed
Fix sonarcube
1 parent d75474d commit 12928bc

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

src/main/java/com/bakdata/kserve/client/KServeClientV2.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,35 +39,37 @@
3939
@Slf4j
4040
public class KServeClientV2<T> extends KServeClient<InferenceRequest<T>> {
4141

42+
public static final String DETAIL_FIELD = "detail";
43+
private static final String ERROR_FIELD = "error";
44+
4245
@Builder
4346
KServeClientV2(
4447
final URL serviceBaseUrl, final String modelName, final OkHttpClient httpClient) {
4548
super(serviceBaseUrl, modelName, httpClient);
4649
}
4750

51+
private static String extractDetailMessage(final JsonNode detail) {
52+
return detail.isArray() && !detail.isEmpty() ? detail.toString() : detail.asText();
53+
}
54+
55+
private static String extractErrorFromParsedBody(final JsonNode root, final String stringBody) {
56+
final JsonNode errorNode = root.get(ERROR_FIELD);
57+
if (errorNode != null && !errorNode.isNull()) {
58+
return errorNode.asText();
59+
}
60+
if (root.has(DETAIL_FIELD)) {
61+
return extractDetailMessage(root.get(DETAIL_FIELD));
62+
}
63+
return "Unknown error occurred. Raw body: " + stringBody;
64+
}
65+
4866
@Override
4967
protected String extractErrorMessage(final String stringBody) {
5068
try {
5169
final JsonNode root = OBJECT_MAPPER.readTree(stringBody);
52-
53-
if (root.has("error") && !root.get("error").isNull()) {
54-
return root.get("error").asText();
55-
}
56-
57-
if (root.has("detail")) {
58-
final JsonNode detail = root.get("detail");
59-
60-
if (detail.isArray() && !detail.isEmpty()) {
61-
return detail.toString();
62-
}
63-
64-
return detail.asText();
65-
}
66-
67-
return "Unknown error occurred. Raw body: " + stringBody;
68-
70+
return extractErrorFromParsedBody(root, stringBody);
6971
} catch (final JsonProcessingException e) {
70-
log.warn("Could not parse error body as JSON: {}", stringBody);
72+
log.warn("Could not parse error body as JSON: {}", stringBody, e);
7173
return stringBody;
7274
}
7375
}

0 commit comments

Comments
 (0)