[web] Only log exceptions in debug mode in Ajax controllers
authorDenis Laxalde <denis.laxalde@logilab.fr>
Mon, 19 Jun 2017 18:00:26 +0200
changeset 12187 3a1f742b84cd
parent 12183 af5d0a3c3f1a
child 12188 fea018b2e056
[web] Only log exceptions in debug mode in Ajax controllers When we raise a RemoteCallFailed error (erroneously turned into a '500 Internal Server Error' response, but that's another business). In production environment, we do not want to log the exception in most cases where it's actually a client error. So only log the exception in debug mode.
cubicweb/web/views/ajaxcontroller.py
--- a/cubicweb/web/views/ajaxcontroller.py	Fri Apr 28 09:49:37 2017 +0200
+++ b/cubicweb/web/views/ajaxcontroller.py	Mon Jun 19 18:00:26 2017 +0200
@@ -133,6 +133,7 @@
             warn('[3.15] remote function %s found on JSonController, '
                  'use AjaxFunction / @ajaxfunc instead' % fname,
                  DeprecationWarning, stacklevel=2)
+        debug_mode = self._cw.vreg.config.debugmode
         # no <arg> attribute means the callback takes no argument
         args = self._cw.form.get('arg', ())
         if not isinstance(args, (list, tuple)):
@@ -140,16 +141,19 @@
         try:
             args = [json.loads(arg) for arg in args]
         except ValueError as exc:
-            self.exception('error while decoding json arguments for '
-                           'js_%s: %s (err: %s)', fname, args, exc)
+            if debug_mode:
+                self.exception('error while decoding json arguments for '
+                               'js_%s: %s (err: %s)', fname, args, exc)
             raise RemoteCallFailed(exc_message(exc, self._cw.encoding))
         try:
             result = func(*args)
         except (RemoteCallFailed, DirectResponse):
             raise
         except Exception as exc:
-            self.exception('an exception occurred while calling js_%s(%s): %s',
-                           fname, args, exc)
+            if debug_mode:
+                self.exception(
+                    'an exception occurred while calling js_%s(%s): %s',
+                    fname, args, exc)
             raise RemoteCallFailed(exc_message(exc, self._cw.encoding))
         if result is None:
             return ''
@@ -219,7 +223,8 @@
         try:
             return self._cw.execute(rql, args)
         except Exception as ex:
-            self.exception("error in _exec(rql=%s): %s", rql, ex)
+            if self._cw.vreg.config.debugmode:
+                self.exception("error in _exec(rql=%s): %s", rql, ex)
             return None
         return None