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

Informative msgs when JsonPatchException is thrown #103

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ buildscript {
repositories {
mavenCentral()
maven {
url "http://repo.springsource.org/plugins-release";
url "https://repo.springsource.org/plugins-release";
}
}
dependencies {
Expand All @@ -31,7 +31,7 @@ buildscript {
};

plugins {
id("net.ltgt.errorprone") version "0.8.1" apply false
id("net.ltgt.errorprone") version "1.3.0" apply false
}

configure(allprojects) {
Expand Down
23 changes: 16 additions & 7 deletions src/main/java/com/github/fge/jsonpatch/AddOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,20 @@ public JsonNode apply(final JsonNode node)
*/
final JsonNode parentNode = path.parent().path(node);
if (parentNode.isMissingNode())
throw new JsonPatchException(BUNDLE.getMessage(
"jsonPatch.noSuchParent"));
if (!parentNode.isContainerNode())
throw new JsonPatchException(BUNDLE.getMessage(
"jsonPatch.parentNotContainer"));
{
String msg = BUNDLE.getMessage("jsonPatch.noSuchParent");
if(node!=null) {
msg = node.toString() + " " + msg;
}
throw new JsonPatchException(msg);
}
if (!parentNode.isContainerNode()) {
String msg = BUNDLE.getMessage("jsonPatch.parentNotContainer");
if(node!=null) {
msg = node.toString() + " " + msg;
}
throw new JsonPatchException(msg);
}
return parentNode.isArray()
? addToArray(path, node)
: addToObject(path, node);
Expand All @@ -119,12 +128,12 @@ private JsonNode addToArray(final JsonPointer path, final JsonNode node)
try {
index = Integer.parseInt(token.toString());
} catch (NumberFormatException ignored) {
throw new JsonPatchException(BUNDLE.getMessage(
throw new JsonPatchException("[] " + BUNDLE.getMessage(
"jsonPatch.notAnIndex"));
}

if (index < 0 || index > size)
throw new JsonPatchException(BUNDLE.getMessage(
throw new JsonPatchException(node.toString() + " " + BUNDLE.getMessage(
"jsonPatch.noSuchIndex"));

target.insert(index, value);
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/github/fge/jsonpatch/CopyOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ public JsonNode apply(final JsonNode node)
throws JsonPatchException
{
final JsonNode dupData = from.path(node).deepCopy();
if (dupData.isMissingNode())
throw new JsonPatchException(BUNDLE.getMessage(
"jsonPatch.noSuchPath"));
if(dupData.isMissingNode()){
String msg = BUNDLE.getMessage("jsonPatch.noSuchPath");
if(dupData!=null) {
msg = node.toString() + " " + msg;
}
throw new JsonPatchException(msg);
}
return new AddOperation(path, dupData).apply(node);
}
}
30 changes: 26 additions & 4 deletions src/main/java/com/github/fge/jsonpatch/MoveOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,33 @@ public JsonNode apply(final JsonNode node)
if (from.equals(path))
return node.deepCopy();
final JsonNode movedNode = from.path(node);
if (movedNode.isMissingNode())
throw new JsonPatchException(BUNDLE.getMessage(
"jsonPatch.noSuchPath"));
if (movedNode.isMissingNode()) {
String msg = BUNDLE.getMessage("jsonPatch.noSuchPath");
if(node!=null) {
msg = node.toString() + " " + msg;
}
throw new JsonPatchException(msg);
}
final JsonPatchOperation remove = new RemoveOperation(from);
final JsonPatchOperation add = new AddOperation(path, movedNode);
return add.apply(remove.apply(node));
JsonNode returnNode = null;
try {
returnNode = add.apply(remove.apply(node));
}
catch(JsonPatchException jpe)
{
String msg = BUNDLE.getMessage("jsonPatch.noSuchParent");
if(jpe.getMessage().contains(msg))
{
// Rethrow the JsonPatchException with a new message
msg = node.toString() + " " + msg;
throw new JsonPatchException(msg);
}
else
{
throw jpe;
}
}
return returnNode;
}
}
9 changes: 7 additions & 2 deletions src/main/java/com/github/fge/jsonpatch/RemoveOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,13 @@ public JsonNode apply(final JsonNode node)
if (path.isEmpty())
return MissingNode.getInstance();
if (path.path(node).isMissingNode())
throw new JsonPatchException(BUNDLE.getMessage(
"jsonPatch.noSuchPath"));
{
String msg = BUNDLE.getMessage("jsonPatch.noSuchPath");
if(node!=null) {
msg = node.toString() + " " + msg;
}
throw new JsonPatchException(msg);
}
final JsonNode ret = node.deepCopy();
final JsonNode parentNode = path.parent().get(ret);
final String raw = Iterables.getLast(path).getToken().getRaw();
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/github/fge/jsonpatch/ReplaceOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,13 @@ public JsonNode apply(final JsonNode node)
* If remove is done first, the array is empty and add rightly complains
* that there is no such index in the array.
*/
if (path.path(node).isMissingNode())
throw new JsonPatchException(BUNDLE.getMessage(
"jsonPatch.noSuchPath"));
if (path.path(node).isMissingNode()) {
String msg = BUNDLE.getMessage("jsonPatch.noSuchPath");
if(node!=null) {
msg = node.toString() + " " + msg;
}
throw new JsonPatchException(msg);
}
final JsonNode replacement = value.deepCopy();
if (path.isEmpty())
return replacement;
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/com/github/fge/jsonpatch/TestOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,15 @@ public JsonNode apply(final JsonNode node)
throws JsonPatchException
{
final JsonNode tested = path.path(node);
if (tested.isMissingNode())
throw new JsonPatchException(BUNDLE.getMessage(
"jsonPatch.noSuchPath"));
if (tested.isMissingNode()) {
String msg = BUNDLE.getMessage("jsonPatch.noSuchPath");
if(node!=null) {
msg = node.toString() + " " + msg;
}
throw new JsonPatchException(msg);
}
if (!EQUIVALENCE.equivalent(tested, value))
throw new JsonPatchException(BUNDLE.getMessage(
throw new JsonPatchException(node.toString() + " " + BUNDLE.getMessage(
"jsonPatch.valueTestFailure"));
return node.deepCopy();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,15 @@ public final void errorsAreCorrectlyReported(final JsonNode patch,
throws IOException
{
final JsonPatchOperation op = reader.readValue(patch);

try {
op.apply(node);
fail("No exception thrown!!");
} catch (JsonPatchException e) {
assertEquals(e.getMessage(), message);
String msg = message;
if(node!=null) {
msg = node.toString() + " " + msg;
}
assertEquals(e.getMessage(), msg);
}
}

Expand Down