From 7bd503574b3d2ec1b6f791ec8abac8629a74c759 Mon Sep 17 00:00:00 2001
From: felmer <felmer>
Date: Tue, 22 May 2012 09:06:15 +0000
Subject: [PATCH] SP-32, BIS-14: Evaluator: Add script line and causing
 exception in EvaluatorException

SVN: 25324
---
 .../cisd/common/evaluator/Evaluator.java      | 23 ++++++++++---------
 .../cisd/common/evaluator/EvaluatorTest.java  |  8 +++----
 2 files changed, 16 insertions(+), 15 deletions(-)

diff --git a/common/source/java/ch/systemsx/cisd/common/evaluator/Evaluator.java b/common/source/java/ch/systemsx/cisd/common/evaluator/Evaluator.java
index 499e168601b..991eb064582 100644
--- a/common/source/java/ch/systemsx/cisd/common/evaluator/Evaluator.java
+++ b/common/source/java/ch/systemsx/cisd/common/evaluator/Evaluator.java
@@ -37,8 +37,10 @@ import org.python.core.PyObject;
 import org.python.core.PyString;
 import org.python.core.PyStringMap;
 import org.python.core.PySystemState;
+import org.python.core.PyTraceback;
 import org.python.util.PythonInterpreter;
 
+import ch.systemsx.cisd.common.shared.basic.utils.CommaSeparatedListBuilder;
 import ch.systemsx.cisd.common.utilities.PythonUtils;
 
 /**
@@ -189,13 +191,9 @@ public final class Evaluator
             return translateToJava(result);
         } catch (PyException ex)
         {
-            StringBuilder builder = new StringBuilder();
+            CommaSeparatedListBuilder builder = new CommaSeparatedListBuilder();
             for (Object argument : args)
             {
-                if (builder.length() > 0)
-                {
-                    builder.append(", ");
-                }
                 builder.append(argument);
             }
             throw toEvaluatorException(ex, functionName + "(" + builder + ")");
@@ -473,20 +471,23 @@ public final class Evaluator
 
     private static EvaluatorException toEvaluatorException(PyException ex, String expressionOrNull)
     {
-        Exception exception = null;
+        Throwable exception = ex;
         PyObject value = ex.value;
         Object object = value.__tojava__(Object.class);
-        if (object instanceof Exception)
+        if (object instanceof Throwable)
         {
-            exception = (Exception) object;
+            exception = (Throwable) object;
         }
         String msg = extractExceptionMessage(ex);
         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,
-                exception);
+        return new EvaluatorException(msg, exception);
     }
 
     private static String extractExceptionMessage(PyException ex)
diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/evaluator/EvaluatorTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/evaluator/EvaluatorTest.java
index 77d6e320df1..9a84d5e14a1 100644
--- a/common/sourceTest/java/ch/systemsx/cisd/common/evaluator/EvaluatorTest.java
+++ b/common/sourceTest/java/ch/systemsx/cisd/common/evaluator/EvaluatorTest.java
@@ -260,9 +260,8 @@ public class EvaluatorTest extends AssertJUnit
             fail("EvaluatorException expected");
         } catch (EvaluatorException ex)
         {
-            assertEquals(
-                    "Error evaluating 'hello(world)': NameError: global name 'unknown' is not defined",
-                    ex.getMessage());
+            assertEquals("Error occurred in line 2 of the script when evaluating 'hello(world)': "
+                    + "NameError: global name 'unknown' is not defined", ex.getMessage());
         }
     }
 
@@ -350,7 +349,8 @@ public class EvaluatorTest extends AssertJUnit
             fail("EvaluatorException expected");
         } 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());
         }
     }
-- 
GitLab