Browse Source

Improved Exception handling.

pull/10/merge
Kalle Stenflo 11 years ago
parent
commit
9ea098d74c
  1. 6
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
  2. 27
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayIndexFilter.java
  3. 9
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/PathTokenFilter.java
  4. 3
      json-path/src/test/java/com/jayway/jsonpath/IssuesTest.java
  5. 3
      json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java

6
json-path/src/main/java/com/jayway/jsonpath/JsonPath.java

@ -255,11 +255,11 @@ public class JsonPath {
result = filter.filter(result, configuration, contextFilters, inArrayContext); result = filter.filter(result, configuration, contextFilters, inArrayContext);
/*
if(result == null && !pathToken.isEndToken()){ if(result == null && !pathToken.isEndToken()){
throw new PathNotFoundException("Path token: " + pathToken.getFragment() + " not found in json"); throw new PathNotFoundException("Path token: '" + pathToken.getFragment() + "' not found.");
} }
*/
if (!inArrayContext) { if (!inArrayContext) {
inArrayContext = filter.isArrayFilter(); inArrayContext = filter.isArrayFilter();

27
json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayIndexFilter.java

@ -15,6 +15,7 @@
package com.jayway.jsonpath.internal.filter; package com.jayway.jsonpath.internal.filter;
import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.PathNotFoundException;
import com.jayway.jsonpath.spi.JsonProvider; import com.jayway.jsonpath.spi.JsonProvider;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -101,20 +102,30 @@ public class ArrayIndexFilter extends PathTokenFilter {
} else { } else {
String[] indexArr = COMMA.split(trimmedCondition); String[] indexArr = COMMA.split(trimmedCondition);
if(obj == null || jsonProvider.length(obj) == 0){ //if(obj == null || jsonProvider.length(obj) == 0){
//throw new PathNotFoundException("Failed to access array index: '" + condition + "' since the array is null or empty"); if(obj == null){
return result; return result;
} }
if (indexArr.length == 1) { try {
return jsonProvider.getProperty(obj, indexArr[0]); if (indexArr.length == 1) {
/*
if(jsonProvider.length(obj) == 0){
throw new PathNotFoundException("Array index [" + indexArr[0] + "] not found in path");
}
*/
} else { return jsonProvider.getProperty(obj, indexArr[0]);
for (String idx : indexArr) { } else {
jsonProvider.setProperty(result, jsonProvider.length(result), jsonProvider.getProperty(obj, idx.trim())); for (String idx : indexArr) {
jsonProvider.setProperty(result, jsonProvider.length(result), jsonProvider.getProperty(obj, idx.trim()));
}
return result;
} }
return result; } catch (IndexOutOfBoundsException e){
throw new PathNotFoundException("Array index " + indexArr + " not found in path", e);
} }
} }
} }

9
json-path/src/main/java/com/jayway/jsonpath/internal/filter/PathTokenFilter.java

@ -58,6 +58,13 @@ public abstract class PathTokenFilter {
@Override @Override
public String toString() { public String toString() {
return getClass().getSimpleName() + " => " + condition;
if(isArrayFilter()){
return getClass().getSimpleName() + " => " + condition;
} else {
return getClass().getSimpleName() + " => '" + condition + "'";
}
} }
} }

3
json-path/src/test/java/com/jayway/jsonpath/IssuesTest.java

@ -25,8 +25,7 @@ import static org.junit.Assert.assertThat;
*/ */
public class IssuesTest { public class IssuesTest {
//@Test(expected = PathNotFoundException.class) @Test(expected = PathNotFoundException.class)
@Test()
public void issue_11() throws Exception { public void issue_11() throws Exception {
String json = "{ \"foo\" : [] }"; String json = "{ \"foo\" : [] }";
List<String> result = JsonPath.read(json, "$.foo[?(@.rel= 'item')][0].uri"); List<String> result = JsonPath.read(json, "$.foo[?(@.rel= 'item')][0].uri");

3
json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java

@ -308,7 +308,7 @@ public class JsonPathTest {
List<String> all = JsonPath.read(DOCUMENT, "$..*"); List<String> all = JsonPath.read(DOCUMENT, "$..*");
} }
@Test(expected = IndexOutOfBoundsException.class) @Test(expected = PathNotFoundException.class)
public void access_index_out_of_bounds_does_not_throw_exception() throws Exception { public void access_index_out_of_bounds_does_not_throw_exception() throws Exception {
Object res = JsonPath.read(DOCUMENT, "$.store.book[100].author"); Object res = JsonPath.read(DOCUMENT, "$.store.book[100].author");
@ -317,4 +317,5 @@ public class JsonPathTest {
} }

Loading…
Cancel
Save