Skip to content

Commit 1dcffdb

Browse files
committed
Produce better error messages in case of validation or formatting
failure
1 parent eda176d commit 1dcffdb

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

core/src/main/java/com/cosium/code/format/FormatCodeMojo.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ protected void process(CodeFormatters codeFormatters, Path path) {
2727
}
2828

2929
private void format(Path path, CodeFormatter formatter) {
30-
getLog().debug("Formatting '" + gitBaseDir().relativize(path) + "'");
30+
Path relativePath = gitBaseDir().relativize(path);
31+
getLog().debug("Formatting '" + relativePath + "'");
3132

3233
try (TemporaryFile temporaryFormattedFile =
3334
TemporaryFile.create(getLog(), path + ".formatted")) {
@@ -40,8 +41,9 @@ private void format(Path path, CodeFormatter formatter) {
4041
OutputStream unformattedContent = Files.newOutputStream(path)) {
4142
IOUtils.copy(formattedContent, unformattedContent);
4243
}
43-
} catch (IOException e) {
44-
throw new MavenGitCodeFormatException(e);
44+
} catch (IOException | RuntimeException e) {
45+
throw new MavenGitCodeFormatException(
46+
String.format("Failed to format '%s': %s", relativePath, e.getMessage()), e);
4547
}
4648

4749
getLog().debug("Formatted '" + gitBaseDir().relativize(path) + "'");

core/src/main/java/com/cosium/code/format/ValidateCodeFormat.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ private boolean validate(CodeFormatters codeFormatters, Path path) {
3333
}
3434

3535
private boolean doValidate(Path path, CodeFormatter formatter) {
36-
getLog().debug("Validating '" + gitBaseDir().relativize(path) + "'");
36+
Path relativePath = gitBaseDir().relativize(path);
37+
getLog().debug("Validating '" + relativePath + "'");
3738
try (InputStream content = Files.newInputStream(path)) {
3839
return formatter.validate(content);
39-
} catch (IOException e) {
40-
throw new MavenGitCodeFormatException(e);
40+
} catch (IOException | RuntimeException e) {
41+
throw new MavenGitCodeFormatException(
42+
String.format("Failed to validate '%s': %s", relativePath, e.getMessage()), e);
4143
}
4244
}
4345
}

0 commit comments

Comments
 (0)