Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ResponseStatusExceptionFilter: Fix unwrap of JSON message #1223

Merged
merged 1 commit into from
Jul 16, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import javax.ws.rs.core.MediaType;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -31,6 +33,8 @@
*/
public class ResponseStatusExceptionFilter implements ClientResponseFilter {

private static final Logger LOG = LoggerFactory.getLogger(ResponseStatusExceptionFilter.class);

@Override
public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {
int status = responseContext.getStatus();
Expand Down Expand Up @@ -79,13 +83,19 @@ private String getBodyAsMessage(ClientResponseContext responseContext) {
String message = IOUtils.toString(entityStream, charset);

if (MediaType.APPLICATION_JSON_TYPE.equals(mediaType)) {
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(entityStream).get("message");
if (node != null) {
message = node.textValue();
try {
JsonNode node = new ObjectMapper().readTree(message);
if (node != null) {
JsonNode messageNode = node.get("message");
if (messageNode != null && messageNode.isTextual()) {
message = messageNode.textValue();
}
}
} catch (IOException e) {
// ignore parsing errors and return the message as is
LOG.debug("Failed to unwrap error message: {}", e.getMessage(), e);
}
}

return message;
} catch (Exception ignored) { }
}
Expand Down