Skip to content

Commit

Permalink
Improve JsonSimple exception messages (#885)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg committed Jul 6, 2021
2 parents eb8004c + bb80062 commit 7028493
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -10,6 +10,8 @@ This document is intended for Spotless developers.
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

## [Unreleased]
### Changed
* Improved exception messages for [JSON formatting](https://github.com/diffplug/spotless/pull/885) failures

## [2.15.0] - 2021-06-17

Expand Down
37 changes: 16 additions & 21 deletions lib/src/main/java/com/diffplug/spotless/json/JsonSimpleStep.java
Expand Up @@ -70,34 +70,29 @@ FormatterFunc toFormatter() {
}

return s -> {
String prettyPrinted = null;
if (s.isEmpty()) {
prettyPrinted = s;
return s;
}
if (s.startsWith("{")) {
try {
Object parsed = objectConstructor.newInstance(s);
prettyPrinted = objectToString.invoke(parsed, indentSpaces) + "\n";
} catch (InvocationTargetException ignored) {
// ignore if we cannot convert to JSON string
}
char first = s.charAt(0);
if (first == '{') {
return format(objectConstructor, objectToString, s);
}
if (s.startsWith("[")) {
try {
Object parsed = arrayConstructor.newInstance(s);
prettyPrinted = arrayToString.invoke(parsed, indentSpaces) + "\n";
} catch (InvocationTargetException ignored) {
// ignore if we cannot convert to JSON string
}
if (first == '[') {
return format(arrayConstructor, arrayToString, s);
}

if (prettyPrinted == null) {
throw new AssertionError("Invalid JSON file provided");
}

return prettyPrinted;
throw new AssertionError(String.format("Unable to determine JSON type, expected a '{' or '[' but found '%s'", first));
};
}

private String format(Constructor<?> constructor, Method toString, String input) throws Exception {
try {
Object parsed = constructor.newInstance(input);
return toString.invoke(parsed, indentSpaces) + "\n";
} catch (InvocationTargetException ex) {
throw new AssertionError("Unable to format JSON", ex.getCause());
}
}
}

private JsonSimpleStep() {
Expand Down
2 changes: 2 additions & 0 deletions plugin-gradle/CHANGES.md
Expand Up @@ -3,6 +3,8 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).

## [Unreleased]
### Changed
* Improved exception messages for [JSON formatting](https://github.com/diffplug/spotless/pull/885) failures

## [5.14.0] - 2021-06-17

Expand Down
Expand Up @@ -73,12 +73,18 @@ public void handlesObjectWithNull() throws Exception {

@Test
public void handlesInvalidJson() {
assertThatThrownBy(() -> doWithResource(stepHarness, "invalidJson")).isInstanceOf(AssertionError.class).hasMessage("Invalid JSON file provided");
assertThatThrownBy(() -> doWithResource(stepHarness, "invalidJson"))
.isInstanceOf(AssertionError.class)
.hasMessage("Unable to format JSON")
.hasRootCauseMessage("Expected a ',' or '}' at 9 [character 0 line 3]");
}

@Test
public void handlesNotJson() {
assertThatThrownBy(() -> doWithResource(stepHarness, "notJson")).isInstanceOf(AssertionError.class).hasMessage("Invalid JSON file provided");
assertThatThrownBy(() -> doWithResource(stepHarness, "notJson"))
.isInstanceOf(AssertionError.class)
.hasMessage("Unable to determine JSON type, expected a '{' or '[' but found '#'")
.hasNoCause();
}

@Test
Expand Down

0 comments on commit 7028493

Please sign in to comment.