close
Skip to content
Merged
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
Improve JsonSimple exception messages
  • Loading branch information
goughy000 committed Jul 6, 2021
commit bb80062e96ff35fec15a0455b2aa3c50b8c00c3a
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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