Skip to content

Commit

Permalink
Wrap all uncaught exceptions during AST processing as assertion error…
Browse files Browse the repository at this point in the history
…s, so the associated node is logged.

	Change on 2016/11/17 by tball <tball@google.com>

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=139472808
  • Loading branch information
tomball committed Nov 17, 2016
1 parent d07a94b commit a852cb6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion translator/Makefile
Expand Up @@ -154,7 +154,7 @@ JAVA_SOURCES = \
ast/TreeNode.java \
ast/TreeUtil.java \
ast/TreeVisitor.java \
ast/TreeVisitorAssertionError.java \
ast/TreeVisitorError.java \
ast/TryStatement.java \
ast/Type.java \
ast/TypeDeclaration.java \
Expand Down
Expand Up @@ -95,11 +95,11 @@ public final void accept(TreeVisitor visitor) {
acceptInner(visitor);
}
visitor.postVisit(this);
} catch (TreeVisitorAssertionError e) {
} catch (TreeVisitorError e) {
// Avoid re-wrapping.
throw e;
} catch (AssertionError e) {
throw new TreeVisitorAssertionError(e, this);
} catch (Throwable t) {
throw new TreeVisitorError(t, this);
}
}

Expand Down
Expand Up @@ -17,22 +17,26 @@
/**
* Wraps an assertion error with information about the node where the error occurred.
*/
public class TreeVisitorAssertionError extends AssertionError {
public class TreeVisitorError extends AssertionError {

TreeVisitorAssertionError(AssertionError original, TreeNode node) {
TreeVisitorError(Throwable original, TreeNode node) {
super(constructMessage(original, node), original.getCause());
setStackTrace(original.getStackTrace());
for (Throwable t : original.getSuppressed()) {
addSuppressed(t);
}
}

private static String constructMessage(AssertionError original, TreeNode node) {
private static String constructMessage(Throwable original, TreeNode node) {
CompilationUnit unit = TreeUtil.getCompilationUnit(node);
String msg = original.getMessage();
if (msg == null || msg.isEmpty()) {
msg = original.getClass().getSimpleName();
}
if (unit == null) {
return original.getMessage();
return msg;
}
return String.format(
"%s:%s: %s", unit.getSourceFilePath(), node.getLineNumber(), original.getMessage());
"%s:%s: %s", unit.getSourceFilePath(), node.getLineNumber(), msg);
}
}

0 comments on commit a852cb6

Please sign in to comment.