Commits

Anonymous committed c4a2481

improved the error messages. put the traceback in the system error.

Comments (0)

Files changed (3)

 
 highlight_language = 'rst'
 
+keep_warnings = True  # this is needed to see the traceback
+
 
 # -- Options for HTML output ---------------------------------------------------
 
 Error handling
 --------------
 
+.. note::
+
+   To see errors in Sphinx, you will need to set
+   ``keep_warnings = True`` in ``conf.py``.
+
+
 Error in ``:setup:``
 ^^^^^^^^^^^^^^^^^^^^
 ::
 ::
 
     .. spreadsheet::
-       :eq: {1} = {0}
+       :eq: {1} = this is error!
             {1:this is error, too} = 1
 
        == ==
     return deflist
 
 
-def safecall(tb_temp='%s'):
+def safecall(func):
     """
     Decorator to call a function w/o a system error
 
         will be returned in 2-tuple always.  (Default: False)
 
 
-    >>> @safecall()
+    >>> @safecall
     ... def somethingwrong(wrong):
     ...     if wrong == 'wrong':
     ...         raise Exception(wrong)
     Exception: wrong
 
     """
+    from functools import wraps
 
-    def decorator(func):
-        from functools import wraps
-
-        @wraps(func)
-        def wrapper(*args, **kwds):
-            mix = kwds.pop('_mix', True)
-            if kwds.pop('_debug', False):
+    @wraps(func)
+    def wrapper(*args, **kwds):
+        mix = kwds.pop('_mix', True)
+        if kwds.pop('_debug', False):
+            result = func(*args, **kwds)
+            tb = None
+        else:
+            try:
                 result = func(*args, **kwds)
                 tb = None
-            else:
-                try:
-                    result = func(*args, **kwds)
-                    tb = None
-                except:
-                    import traceback
-                    import cgi
-                    result = None
-                    tb = tb_temp % cgi.escape(traceback.format_exc())
-            if mix:
-                return result if tb is None else tb
-            else:
-                return (result, tb)
+            except:
+                import traceback
+                result = None
+                tb = traceback.format_exc()
+        if mix:
+            return result if tb is None else tb
+        else:
+            return (result, tb)
 
-        return wrapper
-    return decorator
+    return wrapper
 
 
 class ScopeSetup(object):
         return default
 
     @staticmethod
-    @safecall()
+    @safecall
     def _get_scope(code):
         scope = {}
         exec code in scope
 
 
 empty_setup = ScopeSetup()
-safe_eval = safecall()(eval)
+safe_eval = safecall(eval)
 
 
 def parse_format(argument):
 
 
 def error_with_tb(tb, error, msg):
-    tb_node = nodes.literal_block()
-    tb_node += nodes.Text(tb)
-    return [new_paragraph(msg), tb_node, error(msg)]
+    tb_node = nodes.literal_block(tb, tb)
+    return [error(msg, tb_node)]
 
 
 class SpreadSheet(RSTTable):
                                 cond_tb,
                                 self.state.reporter.error,
                                 "Following error occurs while "
-                                "validating the condition "
-                                "{0}-th row, {1}-th col, cond: '{2}'"
+                                "validating the condition of the cell "
+                                "in {0}-th row, {1}-th col: '{2}'"
                                 .format(irow, i, cond))
                     else:
                         cond_result = True
                                 tb,
                                 self.state.reporter.error,
                                 "Following error occurs while "
-                                "validating "
-                                "{0}-th row, {1}-th col, eq: '{2}'"
+                                "validating the equation of the cell "
+                                "in {0}-th row, {1}-th col: '{2}'"
                                 .format(irow, i, eq))
                 # store cols
                 for (i, c) in enumerate(coldata):