Skip to content

Commit

Permalink
get stack trace right for error() calls (fix #40)
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles Loomis committed Oct 1, 2013
1 parent 732e58d commit 18bf338
Show file tree
Hide file tree
Showing 8 changed files with 562 additions and 486 deletions.
20 changes: 7 additions & 13 deletions panc/src/main/java/org/quattor/pan/dml/functions/ErrorMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ Centre National de la Recherche Scientifique (CNRS).

import static org.quattor.pan.utils.MessageUtils.MSG_ONE_STRING_ARG_REQ;
import static org.quattor.pan.utils.MessageUtils.MSG_RESTRICTED_CONTEXT;

import java.io.File;
import static org.quattor.pan.utils.MessageUtils.MSG_USER_INITIATED_ERROR;
import static org.quattor.pan.utils.MessageUtils.MSG_USER_INITIATED_ERROR_WITHOUT_STRING;

import org.quattor.pan.dml.Operation;
import org.quattor.pan.dml.data.Element;
Expand Down Expand Up @@ -68,20 +68,14 @@ public Element execute(Context context) {

Element result = ops[0].execute(context);

File objectFile = (context.getObjectTemplate() != null) ? context
.getObjectTemplate().source : null;

try {
StringProperty sp = (StringProperty) result;
String msg = String.format("[%s] user-initiated error: %s",
context.getObjectName(), sp.getValue());
throw new EvaluationException(msg, getSourceRange(), objectFile,
context.getTraceback(getSourceRange()));
throw EvaluationException.create(getSourceRange(), context,
MSG_USER_INITIATED_ERROR, context.getObjectName(),
sp.getValue());
} catch (ClassCastException cce) {
throw new EvaluationException(
"user-initiated error thrown with non-string argument",
getSourceRange(), objectFile,
context.getTraceback(getSourceRange()));
throw EvaluationException.create(getSourceRange(),
MSG_USER_INITIATED_ERROR_WITHOUT_STRING);
}
}

Expand Down
6 changes: 5 additions & 1 deletion panc/src/main/java/org/quattor/pan/utils/MessageUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -426,9 +426,13 @@ public class MessageUtils {
public final static String MSG_INVALID_TYPE_FOR_ROOT_ELEMENT = "MSG_INVALID_TYPE_FOR_ROOT_ELEMENT";

public final static String MSG_FATAL_DEPRECATION_MSG = "MSG_FATAL_DEPRECATION_MSG";

public final static String MSG_INVALID_FUNCTION_NAME = "MSG_INVALID_FUNCTION_NAME";

public final static String MSG_USER_INITIATED_ERROR_WITHOUT_STRING = "MSG_USER_INITIATED_ERROR_WITHOUT_STRING";

public final static String MSG_USER_INITIATED_ERROR = "MSG_USER_INITIATED_ERROR";

// Name of the resource bundle to use for compiler messages.
private final static String bundleName = "org.quattor.pan.Messages";

Expand Down
4 changes: 3 additions & 1 deletion panc/src/main/resources/org/quattor/pan/Messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,6 @@ MSG_INVALID_FIRST_ARG_FORMAT=first argument to format() must be a string
MSG_INVALID_SYNTAX_ROOT_ELEMENT=syntax error when evaluating root element: {0}
MSG_INVALID_TYPE_FOR_ROOT_ELEMENT=root element must be an nlist, not {0}
MSG_FATAL_DEPRECATION_MSG=deprecation error: {0}
MSG_INVALID_FUNCTION_NAME={0} cannot be used as a function
MSG_INVALID_FUNCTION_NAME={0} cannot be used as a function
MSG_USER_INITIATED_ERROR_WITHOUT_STRING=user-initiated error thrown with non-string argument
MSG_USER_INITIATED_ERROR=[{0}] user-initiated error: {1}
38 changes: 16 additions & 22 deletions panc/src/test/java/org/quattor/pan/JavaCompilerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Centre National de la Recherche Scientifique (CNRS).
import org.quattor.pan.output.Formatter;
import org.quattor.pan.output.FormatterComparator;
import org.quattor.pan.output.PanFormatter;
import org.quattor.pan.utils.TestUtils;
import org.xml.sax.InputSource;

public class JavaCompilerTest {
Expand All @@ -67,8 +68,8 @@ protected Compiler getDefaultCompiler(File tplfile, File dir,
formatters.add(formatter);
formatters.add(DepFormatter.getInstance());
CompilerOptions options = new CompilerOptions(null, null, 100, 50,
formatters, getTmpdir(), path, CompilerOptions.DeprecationWarnings.ON, null, null,
null);
formatters, getTmpdir(), path,
CompilerOptions.DeprecationWarnings.ON, null, null, null);
List<File> tplfiles = new LinkedList<File>();
tplfiles.add(tplfile);
return new Compiler(options, new LinkedList<String>(), tplfiles);
Expand All @@ -83,7 +84,8 @@ protected Compiler getRootElementCompiler(File tplfile, File dir,
formatters.add(formatter);
formatters.add(DepFormatter.getInstance());
CompilerOptions options = new CompilerOptions(null, null, 100, 50,
formatters, getTmpdir(), path, CompilerOptions.DeprecationWarnings.ON, null, null,
formatters, getTmpdir(), path,
CompilerOptions.DeprecationWarnings.ON, null, null,
"nlist('root-element-test', 'OK')");
List<File> tplfiles = new LinkedList<File>();
tplfiles.add(tplfile);
Expand All @@ -100,8 +102,8 @@ protected Compiler getDependencyCompiler(File tplfile, File dir)
formatters.add(formatter);
formatters.add(DepFormatter.getInstance());
CompilerOptions options = new CompilerOptions(null, null, 100, 50,
formatters, getTmpdir(), path, CompilerOptions.DeprecationWarnings.ON, null, null,
null);
formatters, getTmpdir(), path,
CompilerOptions.DeprecationWarnings.ON, null, null, null);
List<File> tplfiles = new LinkedList<File>();
tplfiles.add(tplfile);
return new Compiler(options, new LinkedList<String>(), tplfiles);
Expand All @@ -119,8 +121,8 @@ public void testErrorOnMissingObjectFile() throws SyntaxException {
formatters.add(PanFormatter.getInstance());
formatters.add(DepFormatter.getInstance());
CompilerOptions options = new CompilerOptions(null, null, 100, 50,
formatters, getTmpdir(), path, CompilerOptions.DeprecationWarnings.ON, null, null,
null);
formatters, getTmpdir(), path,
CompilerOptions.DeprecationWarnings.ON, null, null, null);

List<String> objects = new LinkedList<String>();
objects.add("non-existant/object/template");
Expand Down Expand Up @@ -165,8 +167,8 @@ public void testErrorOnMisplacedObjectFile() throws FileNotFoundException,
formatters.add(DepFormatter.getInstance());

CompilerOptions options = new CompilerOptions(null, null, 100, 50,
formatters, tmpdir, path, CompilerOptions.DeprecationWarnings.ON, null, null,
null);
formatters, tmpdir, path,
CompilerOptions.DeprecationWarnings.ON, null, null, null);

// Create the list of input files.
List<File> tplfiles = new LinkedList<File>();
Expand Down Expand Up @@ -347,35 +349,27 @@ protected String invokeTest(File rootdir, File objtpl,
formatter);
Set<Throwable> exceptions = compiler.process().getErrors();

if (expectation instanceof Class<?>) {
if (expectation instanceof TestUtils.ExceptionChecker) {

// Cast to an exception class.
Class<?> exceptionClass = (Class<?>) expectation;
TestUtils.ExceptionChecker checker = (TestUtils.ExceptionChecker) expectation;

// An exception was expected. Ensure that the output XML file does
// not exist.
if (xml.exists()) {
return "expected " + exceptionClass.getSimpleName() + " but "
return "expected " + checker.getExceptionName() + " but "
+ xml.getName() + " exists";
}

// Check that there is at least one exception.
if (exceptions.size() == 0) {
return ("expected " + exceptionClass.getSimpleName() + " but no exception was thrown");
return ("expected " + checker.getExceptionName() + " but no exception was thrown");
}

// There shouldn't be more than one exception, but check
// them all if there is. The type should be the exception that was
// indicated in the file.
for (Throwable t : exceptions) {
try {
exceptionClass.cast(t);
} catch (ClassCastException cce) {
t.printStackTrace();
return ("expected " + exceptionClass.getSimpleName()
+ " but got " + t.getClass().getSimpleName());
}
}
return checker.check(exceptions);

} else if (expectation instanceof XPathExpression) {

Expand Down
Loading

0 comments on commit 18bf338

Please sign in to comment.