From 9dd0cd9fb35ba82728346759c68974ea09df7d91 Mon Sep 17 00:00:00 2001 From: Archimedes Trajano Date: Thu, 24 Sep 2015 00:32:18 -0400 Subject: [PATCH 1/3] Provide a constructor that captures the JSON Rather than "debug" the JSON which does not allow further processing for the exception capture and put it inside the exception data. --- .../jayway/jsonpath/InvalidJsonException.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/json-path/src/main/java/com/jayway/jsonpath/InvalidJsonException.java b/json-path/src/main/java/com/jayway/jsonpath/InvalidJsonException.java index 5c660b38..cfad167b 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/InvalidJsonException.java +++ b/json-path/src/main/java/com/jayway/jsonpath/InvalidJsonException.java @@ -17,18 +17,42 @@ package com.jayway.jsonpath; @SuppressWarnings("serial") public class InvalidJsonException extends JsonPathException { + /** + * Problematic JSON if available. + */ + private final String json; + public InvalidJsonException() { + json = null; } public InvalidJsonException(String message) { super(message); + json = null; } public InvalidJsonException(String message, Throwable cause) { super(message, cause); + json = null; } public InvalidJsonException(Throwable cause) { super(cause); + json = null; + } + + /** + * Rethrow the exception with the problematic JSON captured. + */ + public InvalidJsonException(final Throwable cause, final String json) { + super(cause); + this.json = json; + } + + /** + * @return the problematic JSON if available. + */ + public String getJson() { + return json; } } From cba33fba0524eade26b81d90703720a24d58030c Mon Sep 17 00:00:00 2001 From: Archimedes Trajano Date: Thu, 24 Sep 2015 00:34:43 -0400 Subject: [PATCH 2/3] Store problematic JSON in exception Rather than logging the JSON that is problematic store it as part of the exception. Remove the logger since it is no longer being used. --- .../com/jayway/jsonpath/spi/json/JacksonJsonProvider.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/json-path/src/main/java/com/jayway/jsonpath/spi/json/JacksonJsonProvider.java b/json-path/src/main/java/com/jayway/jsonpath/spi/json/JacksonJsonProvider.java index 1d864bdc..ca60048c 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/spi/json/JacksonJsonProvider.java +++ b/json-path/src/main/java/com/jayway/jsonpath/spi/json/JacksonJsonProvider.java @@ -18,8 +18,6 @@ import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectReader; import com.jayway.jsonpath.InvalidJsonException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.io.IOException; import java.io.InputStream; @@ -31,8 +29,6 @@ import java.util.List; public class JacksonJsonProvider extends AbstractJsonProvider { - private static final Logger logger = LoggerFactory.getLogger(JacksonJsonProvider.class); - private static final ObjectMapper defaultObjectMapper = new ObjectMapper(); private static final ObjectReader defaultObjectReader = defaultObjectMapper.reader().withType(Object.class); @@ -73,8 +69,7 @@ public class JacksonJsonProvider extends AbstractJsonProvider { try { return objectReader.readValue(json); } catch (IOException e) { - logger.debug("Invalid JSON: \n" + json); - throw new InvalidJsonException(e); + throw new InvalidJsonException(e, json); } } From b79f0550a22308e6aab1522e9bd323c68ffe1df3 Mon Sep 17 00:00:00 2001 From: Archimedes Trajano Date: Thu, 24 Sep 2015 00:35:35 -0400 Subject: [PATCH 3/3] Store problematic JSON in exception Rather than logging the JSON that is problematic store it as part of the exception. Remove the logger since it is no longer being used. --- .../jsonpath/spi/json/JacksonJsonNodeJsonProvider.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/json-path/src/main/java/com/jayway/jsonpath/spi/json/JacksonJsonNodeJsonProvider.java b/json-path/src/main/java/com/jayway/jsonpath/spi/json/JacksonJsonNodeJsonProvider.java index 8ef4cd18..a7d58255 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/spi/json/JacksonJsonNodeJsonProvider.java +++ b/json-path/src/main/java/com/jayway/jsonpath/spi/json/JacksonJsonNodeJsonProvider.java @@ -8,8 +8,6 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.TextNode; import com.jayway.jsonpath.InvalidJsonException; import com.jayway.jsonpath.JsonPathException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.io.IOException; import java.io.InputStream; @@ -21,8 +19,6 @@ import java.util.List; public class JacksonJsonNodeJsonProvider extends AbstractJsonProvider { - private static final Logger logger = LoggerFactory.getLogger(JacksonJsonProvider.class); - private static final ObjectMapper defaultObjectMapper = new ObjectMapper(); protected ObjectMapper objectMapper; @@ -52,8 +48,7 @@ public class JacksonJsonNodeJsonProvider extends AbstractJsonProvider { try { return objectMapper.readTree(json); } catch (IOException e) { - logger.debug("Invalid JSON: \n" + json); - throw new InvalidJsonException(e); + throw new InvalidJsonException(e, json); } }