|
39 | 39 | @Slf4j |
40 | 40 | public class KServeClientV2<T> extends KServeClient<InferenceRequest<T>> { |
41 | 41 |
|
| 42 | + public static final String DETAIL_FIELD = "detail"; |
| 43 | + private static final String ERROR_FIELD = "error"; |
| 44 | + |
42 | 45 | @Builder |
43 | 46 | KServeClientV2( |
44 | 47 | final URL serviceBaseUrl, final String modelName, final OkHttpClient httpClient) { |
45 | 48 | super(serviceBaseUrl, modelName, httpClient); |
46 | 49 | } |
47 | 50 |
|
| 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 | + |
48 | 66 | @Override |
49 | 67 | protected String extractErrorMessage(final String stringBody) { |
50 | 68 | try { |
51 | 69 | 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); |
69 | 71 | } 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); |
71 | 73 | return stringBody; |
72 | 74 | } |
73 | 75 | } |
|
0 commit comments