Skip to content

Commit 32b4db6

Browse files
committed
ResponseStatusExceptionFilter: Unwrap a JSON message
For JSON messages like {"message":"Cannot locate specified Dockerfile: /path/to/Dockerfile"} unwrap the message to make the exception message read nicer.
1 parent 4b3d908 commit 32b4db6

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

src/main/java/com/github/dockerjava/jaxrs/filter/ResponseStatusExceptionFilter.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
import org.apache.commons.io.IOUtils;
1313

14+
import com.fasterxml.jackson.databind.JsonNode;
15+
import com.fasterxml.jackson.databind.ObjectMapper;
16+
1417
import com.github.dockerjava.api.exception.BadRequestException;
1518
import com.github.dockerjava.api.exception.ConflictException;
1619
import com.github.dockerjava.api.exception.DockerException;
@@ -73,7 +76,17 @@ private String getBodyAsMessage(ClientResponseContext responseContext) {
7376
charset = Charset.defaultCharset();
7477
}
7578

76-
return IOUtils.toString(entityStream, charset);
79+
String message = IOUtils.toString(entityStream, charset);
80+
81+
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+
}
87+
}
88+
89+
return message;
7790
} catch (Exception ignored) { }
7891
}
7992
return null;

0 commit comments

Comments
 (0)