Skip to content

Commit bb80062

Browse files
committed
Improve JsonSimple exception messages
1 parent eb8004c commit bb80062

File tree

4 files changed

+28
-23
lines changed

4 files changed

+28
-23
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This document is intended for Spotless developers.
1010
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
1111

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

1416
## [2.15.0] - 2021-06-17
1517

lib/src/main/java/com/diffplug/spotless/json/JsonSimpleStep.java

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -70,34 +70,29 @@ FormatterFunc toFormatter() {
7070
}
7171

7272
return s -> {
73-
String prettyPrinted = null;
7473
if (s.isEmpty()) {
75-
prettyPrinted = s;
74+
return s;
7675
}
77-
if (s.startsWith("{")) {
78-
try {
79-
Object parsed = objectConstructor.newInstance(s);
80-
prettyPrinted = objectToString.invoke(parsed, indentSpaces) + "\n";
81-
} catch (InvocationTargetException ignored) {
82-
// ignore if we cannot convert to JSON string
83-
}
76+
char first = s.charAt(0);
77+
if (first == '{') {
78+
return format(objectConstructor, objectToString, s);
8479
}
85-
if (s.startsWith("[")) {
86-
try {
87-
Object parsed = arrayConstructor.newInstance(s);
88-
prettyPrinted = arrayToString.invoke(parsed, indentSpaces) + "\n";
89-
} catch (InvocationTargetException ignored) {
90-
// ignore if we cannot convert to JSON string
91-
}
80+
if (first == '[') {
81+
return format(arrayConstructor, arrayToString, s);
9282
}
9383

94-
if (prettyPrinted == null) {
95-
throw new AssertionError("Invalid JSON file provided");
96-
}
97-
98-
return prettyPrinted;
84+
throw new AssertionError(String.format("Unable to determine JSON type, expected a '{' or '[' but found '%s'", first));
9985
};
10086
}
87+
88+
private String format(Constructor<?> constructor, Method toString, String input) throws Exception {
89+
try {
90+
Object parsed = constructor.newInstance(input);
91+
return toString.invoke(parsed, indentSpaces) + "\n";
92+
} catch (InvocationTargetException ex) {
93+
throw new AssertionError("Unable to format JSON", ex.getCause());
94+
}
95+
}
10196
}
10297

10398
private JsonSimpleStep() {

plugin-gradle/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).
44

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

79
## [5.14.0] - 2021-06-17
810

testlib/src/test/java/com/diffplug/spotless/json/JsonSimpleStepTest.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,18 @@ public void handlesObjectWithNull() throws Exception {
7373

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

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

8490
@Test

0 commit comments

Comments
 (0)