|
10 | 10 | import javax.ws.rs.core.MediaType; |
11 | 11 |
|
12 | 12 | import org.apache.commons.io.IOUtils; |
| 13 | +import org.slf4j.Logger; |
| 14 | +import org.slf4j.LoggerFactory; |
13 | 15 |
|
14 | 16 | import com.fasterxml.jackson.databind.JsonNode; |
15 | 17 | import com.fasterxml.jackson.databind.ObjectMapper; |
|
31 | 33 | */ |
32 | 34 | public class ResponseStatusExceptionFilter implements ClientResponseFilter { |
33 | 35 |
|
| 36 | + private static final Logger LOG = LoggerFactory.getLogger(ResponseStatusExceptionFilter.class); |
| 37 | + |
34 | 38 | @Override |
35 | 39 | public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException { |
36 | 40 | int status = responseContext.getStatus(); |
@@ -79,13 +83,19 @@ private String getBodyAsMessage(ClientResponseContext responseContext) { |
79 | 83 | String message = IOUtils.toString(entityStream, charset); |
80 | 84 |
|
81 | 85 | if (MediaType.APPLICATION_JSON_TYPE.equals(mediaType)) { |
82 | | - ObjectMapper mapper = new ObjectMapper(); |
83 | | - JsonNode node = mapper.readTree(entityStream).get("message"); |
84 | | - if (node != null) { |
85 | | - message = node.textValue(); |
| 86 | + try { |
| 87 | + JsonNode node = new ObjectMapper().readTree(message); |
| 88 | + if (node != null) { |
| 89 | + JsonNode messageNode = node.get("message"); |
| 90 | + if (messageNode != null && messageNode.isTextual()) { |
| 91 | + message = messageNode.textValue(); |
| 92 | + } |
| 93 | + } |
| 94 | + } catch (IOException e) { |
| 95 | + // ignore parsing errors and return the message as is |
| 96 | + LOG.debug("Failed to unwrap error message: {}", e.getMessage(), e); |
86 | 97 | } |
87 | 98 | } |
88 | | - |
89 | 99 | return message; |
90 | 100 | } catch (Exception ignored) { } |
91 | 101 | } |
|
0 commit comments