[web] Return useful error messages when exceptions arise in ajax controllers (closes #3932503) stable
authorRémi Cardona <remi.cardona@logilab.fr>
Thu, 05 Jun 2014 17:41:14 +0200
branchstable
changeset 9921 8227358aa983
parent 9920 5aa730bf445e
child 9922 fc02ba8654a7
[web] Return useful error messages when exceptions arise in ajax controllers (closes #3932503) The call to super() in RemoteCallException passes 'reason' all the way to python's Exception.__init__() which is then stored in Exception.args. This way, CubicWebPublisher.ajax_error_handler() gets a useful error message when calling unicode(exc). The change to uilib is just to prepend the exception type name. Just a small improvement.
uilib.py
web/_exceptions.py
--- a/uilib.py	Wed Jul 23 19:42:42 2014 +0200
+++ b/uilib.py	Thu Jun 05 17:41:14 2014 +0200
@@ -444,12 +444,14 @@
 
 def exc_message(ex, encoding):
     try:
-        return unicode(ex)
+        excmsg = unicode(ex)
     except Exception:
         try:
-            return unicode(str(ex), encoding, 'replace')
+            excmsg = unicode(str(ex), encoding, 'replace')
         except Exception:
-            return unicode(repr(ex), encoding, 'replace')
+            excmsg = unicode(repr(ex), encoding, 'replace')
+    exctype = unicode(ex.__class__.__name__)
+    return u'%s: %s' % (exctype, excmsg)
 
 
 def rest_traceback(info, exception):
--- a/web/_exceptions.py	Wed Jul 23 19:42:42 2014 +0200
+++ b/web/_exceptions.py	Thu Jun 05 17:41:14 2014 +0200
@@ -102,7 +102,7 @@
     """raised when a json remote call fails
     """
     def __init__(self, reason='', status=httplib.INTERNAL_SERVER_ERROR):
-        super(RemoteCallFailed, self).__init__(status=status)
+        super(RemoteCallFailed, self).__init__(reason, status=status)
         self.reason = reason
 
     def dumps(self):