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

Improve JsonSimple exception messages #885

Merged
merged 1 commit into from Jul 6, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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