Skip to content

Commit

Permalink
Distinguish message and title in the error report (#26)
Browse files Browse the repository at this point in the history
Signed-off-by: Clement Escoffier <[email protected]>
  • Loading branch information
cescoffier committed Jun 4, 2014
1 parent 85efb1a commit d440d32
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class WatchingException extends Exception {

private final int line;
private final int character;
private final String title;

/**
* Creates a Watching Exception.
Expand Down Expand Up @@ -58,11 +59,18 @@ public WatchingException(String message, Throwable cause) {
* @param cause the cause of the error, if known.
*/
public WatchingException(String message, File file, Throwable cause) {
this(message, file, -1, -1, cause);
this("Watching Exception", message, file,
-1, -1, cause);
}

public WatchingException(String message, File file, int line, int character, Throwable cause) {
public WatchingException(String title, String message, File file, Throwable cause) {
this(title, message, file,
-1, -1, cause);
}

public WatchingException(String title, String message, File file, int line, int character, Throwable cause) {
super(message, cause);
this.title = title;
this.file = file;
this.line = line;
this.character = character;
Expand Down Expand Up @@ -96,4 +104,13 @@ public int getCharacter() {
return character;
}

/**
* Gets the title if any.
*
* @return the title
*/
public String getTitle() {
return title;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class CoffeeScriptCompilerMojo extends AbstractWisdomWatcherMojo implemen

public static final String COFFEE_SCRIPT_NPM_NAME = "coffee-script";
public static final String COFFEE_SCRIPT_COMMAND = "coffee";
public static final String ERROR_TITLE = "CoffeeScript Compilation Error";
private File internalSources;
private File destinationForInternals;
private File externalSources;
Expand Down Expand Up @@ -154,16 +155,17 @@ private void invokeCoffeeScriptCompiler(File input, File out) throws WatchingExc
getLog().debug("CoffeeScript compilation exits with " + exit + " status");
} catch (MojoExecutionException e) {
if (!Strings.isNullOrEmpty(coffee.getLastErrorStream())) {
throw build(coffee.getLastErrorStream());
throw build(coffee.getLastErrorStream(), input);
} else {
throw new WatchingException("Error while compiling " + input.getAbsolutePath(), e);
throw new WatchingException(ERROR_TITLE, "Error while compiling " + input
.getAbsolutePath(), input, e);
}
}
}

public static Pattern COFFEE_COMPILATION_ERROR = Pattern.compile("(.*):([0-9]*):([0-9]*):(.*)");

public WatchingException build(String message) {
public WatchingException build(String message, File source) {
String[] lines = message.split("\n");
for (String l : lines) {
if (!Strings.isNullOrEmpty(l)) {
Expand All @@ -178,10 +180,10 @@ public WatchingException build(String message) {
String character = matcher.group(3);
String reason = matcher.group(4);
File file = new File(path);
return new WatchingException("CoffeeScript Compilation Error: " + reason, file,
return new WatchingException(ERROR_TITLE, reason, file,
Integer.valueOf(line), Integer.valueOf(character), null);
} else {
return new WatchingException("CoffeeScript Compilation Error : " + message);
return new WatchingException(ERROR_TITLE, message, source, null);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
public class LessCompilerMojo extends AbstractWisdomWatcherMojo implements Constants {

public static final String LESS_NPM_NAME = "less";
public static final String ERROR_TITLE = "Less Compilation Error";
private File internalSources;
private File destinationForInternals;
private File externalSources;
Expand Down Expand Up @@ -139,7 +140,8 @@ public void compile(File file) throws WatchingException {
}

if (!out.isFile()) {
throw new WatchingException("Error during the compilation of " + file.getAbsoluteFile() + ", check log");
throw new WatchingException(ERROR_TITLE, "Error during the compilation of " + file
.getAbsoluteFile() + "," + " check log", file, null);
}
}

Expand All @@ -156,10 +158,10 @@ private WatchingException buildWatchingException(String stream, File file, MojoE
String line = matcher.group(2);
String character = matcher.group(3);
String reason = matcher.group(1);
return new WatchingException("Less Compilation Error: " + reason, file,
return new WatchingException("Less Compilation Error", reason, file,
Integer.valueOf(line), Integer.valueOf(character), null);
} else {
return new WatchingException("Less Compilation Error : " + stream, e.getCause());
return new WatchingException("Less Compilation Error", stream, file, e.getCause());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ private void createErrorFile(Watcher watcher, WatchingException e) {
if (e.getCause() != null) {
obj.put("cause", e.getCause().getMessage());
}
if (e.getTitle() != null) {
obj.put("title", e.getTitle());
}
try {
FileUtils.writeStringToFile(getErrorFileForWatcher(watcher), obj.toJSONString(), false);
} catch (IOException e1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,12 @@ public static WatchingException createWatchingException(InvocationTargetExceptio
Method line = cause.getClass().getMethod("getLine");
Method character = cause.getClass().getMethod("getCharacter");
Method source = cause.getClass().getMethod("getFile");
Method title = cause.getClass().getMethod("getTitle");

return new WatchingException(cause.getMessage(), (File) source.invoke(cause),
return new WatchingException(
(String) title.invoke(cause),
cause.getMessage(),
(File) source.invoke(cause),
(Integer) line.invoke(cause),
(Integer) character.invoke(cause),
cause.getCause());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class CompilerExecutor {
public static final String DEFAULT_VERSION = "3.1";
public static final String GROUP_ID = "org.apache.maven.plugins";
public static final String COMPILE_GOAL = "compile";
public static final String ERROR_TITLE = "Java Compilation Error: ";

public void execute(AbstractWisdomMojo mojo) throws MojoExecutionException {
String version = PluginExtractor.getBuildPluginVersion(mojo, MAVEN_COMPILER_PLUGIN);
Expand Down Expand Up @@ -116,10 +117,10 @@ public static WatchingException build(AbstractWisdomMojo mojo, Throwable excepti
String character = matcher.group(3);
String reason = matcher.group(4);
File file = new File(path);
return new WatchingException("Java Compilation Error: " + reason, file, Integer.valueOf(line),
return new WatchingException(ERROR_TITLE, reason, file, Integer.valueOf(line),
Integer.valueOf(character), null);
} else {
return new WatchingException("Java Compilation Error", exception);
return new WatchingException(ERROR_TITLE, exception.getMessage(), null, exception);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
<!DOCTYPE html>
<html>
<head>
<title th:text="${message}">
message
<title th:text="${title} ? ${title} : ${message}">
title or message
</title>
<style>
html, body, pre {
Expand Down Expand Up @@ -134,7 +134,11 @@
</style>
</head>
<body id="wisdom-error-page">
<h1 th:text="${message}">ERROR TYPE</h1>
<h1 th:text="${title} ? ${title} : ${message}">The title or message</h1>

<p id="detail" th:if="${title}" th:text="${message}">
The message if the title is set.
</p>

<div th:if="${source}">
<h2>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,13 @@ public BadWatcher(File root, String extension) {

@Override
public boolean fileCreated(File file) throws WatchingException {
throw new WatchingException("Bad bad bad", file, 10, 11, null);
throw new WatchingException("BAD_TITLE", "Bad bad bad", file, 10, 11,
null);
}

@Override
public boolean fileUpdated(File file) throws WatchingException {
throw new WatchingException("Bad bad bad", file, 10, 11, null);
throw new WatchingException("BAD_TITLE", "Bad bad bad", file, 10, 11, null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ private Result renderPipelineError(File error) throws IOException {
line = node.get("line").asInt();
}

String title = null;
if (node.get("title") != null) {
title = node.get("title").asText();
}

if (node.get("character") != null) {
character = node.get("character").asInt();
}
Expand All @@ -284,6 +289,7 @@ private Result renderPipelineError(File error) throws IOException {
}

return internalServerError(render(pipeline,
"title", title,
"message", message,
"source", source,
"line", line,
Expand Down

0 comments on commit d440d32

Please sign in to comment.