diff --git a/CHANGES.md b/CHANGES.md index 3dedb396e7..84b9ff32fe 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 diff --git a/lib/src/main/java/com/diffplug/spotless/json/JsonSimpleStep.java b/lib/src/main/java/com/diffplug/spotless/json/JsonSimpleStep.java index 118f81d0d7..a970149133 100644 --- a/lib/src/main/java/com/diffplug/spotless/json/JsonSimpleStep.java +++ b/lib/src/main/java/com/diffplug/spotless/json/JsonSimpleStep.java @@ -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() { diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 9f36b3b567..144b7676cb 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -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 diff --git a/testlib/src/test/java/com/diffplug/spotless/json/JsonSimpleStepTest.java b/testlib/src/test/java/com/diffplug/spotless/json/JsonSimpleStepTest.java index fccd563444..0bbe1acdca 100644 --- a/testlib/src/test/java/com/diffplug/spotless/json/JsonSimpleStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/json/JsonSimpleStepTest.java @@ -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