Skip to content
Snippets Groups Projects
Commit 7bd50357 authored by felmer's avatar felmer
Browse files

SP-32, BIS-14: Evaluator: Add script line and causing exception in EvaluatorException

SVN: 25324
parent b6c64261
No related branches found
No related tags found
No related merge requests found
...@@ -37,8 +37,10 @@ import org.python.core.PyObject; ...@@ -37,8 +37,10 @@ import org.python.core.PyObject;
import org.python.core.PyString; import org.python.core.PyString;
import org.python.core.PyStringMap; import org.python.core.PyStringMap;
import org.python.core.PySystemState; import org.python.core.PySystemState;
import org.python.core.PyTraceback;
import org.python.util.PythonInterpreter; import org.python.util.PythonInterpreter;
import ch.systemsx.cisd.common.shared.basic.utils.CommaSeparatedListBuilder;
import ch.systemsx.cisd.common.utilities.PythonUtils; import ch.systemsx.cisd.common.utilities.PythonUtils;
/** /**
...@@ -189,13 +191,9 @@ public final class Evaluator ...@@ -189,13 +191,9 @@ public final class Evaluator
return translateToJava(result); return translateToJava(result);
} catch (PyException ex) } catch (PyException ex)
{ {
StringBuilder builder = new StringBuilder(); CommaSeparatedListBuilder builder = new CommaSeparatedListBuilder();
for (Object argument : args) for (Object argument : args)
{ {
if (builder.length() > 0)
{
builder.append(", ");
}
builder.append(argument); builder.append(argument);
} }
throw toEvaluatorException(ex, functionName + "(" + builder + ")"); throw toEvaluatorException(ex, functionName + "(" + builder + ")");
...@@ -473,20 +471,23 @@ public final class Evaluator ...@@ -473,20 +471,23 @@ public final class Evaluator
private static EvaluatorException toEvaluatorException(PyException ex, String expressionOrNull) private static EvaluatorException toEvaluatorException(PyException ex, String expressionOrNull)
{ {
Exception exception = null; Throwable exception = ex;
PyObject value = ex.value; PyObject value = ex.value;
Object object = value.__tojava__(Object.class); Object object = value.__tojava__(Object.class);
if (object instanceof Exception) if (object instanceof Throwable)
{ {
exception = (Exception) object; exception = (Throwable) object;
} }
String msg = extractExceptionMessage(ex); String msg = extractExceptionMessage(ex);
if (expressionOrNull != null) if (expressionOrNull != null)
{ {
msg = "Error evaluating '" + expressionOrNull + "': " + msg; PyTraceback traceback = ex.traceback;
String details =
traceback == null ? "" : "occurred in line " + traceback.tb_lineno
+ " of the script when ";
msg = "Error " + details + "evaluating '" + expressionOrNull + "': " + msg;
} }
return exception == null ? new EvaluatorException(msg) : new EvaluatorException(msg, return new EvaluatorException(msg, exception);
exception);
} }
private static String extractExceptionMessage(PyException ex) private static String extractExceptionMessage(PyException ex)
......
...@@ -260,9 +260,8 @@ public class EvaluatorTest extends AssertJUnit ...@@ -260,9 +260,8 @@ public class EvaluatorTest extends AssertJUnit
fail("EvaluatorException expected"); fail("EvaluatorException expected");
} catch (EvaluatorException ex) } catch (EvaluatorException ex)
{ {
assertEquals( assertEquals("Error occurred in line 2 of the script when evaluating 'hello(world)': "
"Error evaluating 'hello(world)': NameError: global name 'unknown' is not defined", + "NameError: global name 'unknown' is not defined", ex.getMessage());
ex.getMessage());
} }
} }
...@@ -350,7 +349,8 @@ public class EvaluatorTest extends AssertJUnit ...@@ -350,7 +349,8 @@ public class EvaluatorTest extends AssertJUnit
fail("EvaluatorException expected"); fail("EvaluatorException expected");
} catch (EvaluatorException ex) } catch (EvaluatorException ex)
{ {
assertEquals("Error evaluating 'get(world, universe)': AttributeError: " assertEquals("Error occurred in line 2 of the script when evaluating "
+ "'get(world, universe)': AttributeError: "
+ "'str' object has no attribute 'get'", ex.getMessage()); + "'str' object has no attribute 'get'", ex.getMessage());
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment