# HG changeset patch # User Denis Laxalde # Date 1497888928 -7200 # Node ID fea018b2e056c2d1b759c04889dbbccb59796027 # Parent 3a1f742b84cd7d93e68788cb5f5a17a2f7831bef [web] Set response status to 400 when appropriate in ajax controller When this is clearly a client error, set status to 400. Otherwise, keep the default value for RemoteCallFailed (status=500). diff -r 3a1f742b84cd -r fea018b2e056 cubicweb/web/test/unittest_views_basecontrollers.py --- a/cubicweb/web/test/unittest_views_basecontrollers.py Mon Jun 19 18:00:26 2017 +0200 +++ b/cubicweb/web/test/unittest_views_basecontrollers.py Mon Jun 19 18:15:28 2017 +0200 @@ -1012,6 +1012,13 @@ f = appobject(req) self.assertEqual(f(12, 13), '25') + def test_badrequest(self): + with self.assertRaises(RemoteCallFailed) as cm: + with self.remote_calling('foo'): + pass + self.assertEqual(cm.exception.status, 400) + self.assertEqual(cm.exception.reason, 'no foo method') + class JSonControllerTC(AjaxControllerTC): # NOTE: this class performs the same tests as AjaxController but with diff -r 3a1f742b84cd -r fea018b2e056 cubicweb/web/views/ajaxcontroller.py --- a/cubicweb/web/views/ajaxcontroller.py Mon Jun 19 18:00:26 2017 +0200 +++ b/cubicweb/web/views/ajaxcontroller.py Mon Jun 19 18:15:28 2017 +0200 @@ -67,6 +67,7 @@ from functools import partial from six import PY2, text_type +from six.moves import http_client from logilab.common.date import strptime from logilab.common.registry import yes @@ -116,7 +117,8 @@ try: fname = self._cw.form['fname'] except KeyError: - raise RemoteCallFailed('no method specified') + raise RemoteCallFailed('no method specified', + status=http_client.BAD_REQUEST) # 1/ check first for old-style (JSonController) ajax func for bw compat try: func = getattr(basecontrollers.JSonController, 'js_%s' % fname) @@ -128,7 +130,8 @@ try: func = self._cw.vreg['ajax-func'].select(fname, self._cw) except ObjectNotFound: - raise RemoteCallFailed('no %s method' % fname) + raise RemoteCallFailed('no %s method' % fname, + status=http_client.BAD_REQUEST) else: warn('[3.15] remote function %s found on JSonController, ' 'use AjaxFunction / @ajaxfunc instead' % fname, @@ -144,7 +147,8 @@ 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)) + raise RemoteCallFailed(exc_message(exc, self._cw.encoding), + status=http_client.BAD_REQUEST) try: result = func(*args) except (RemoteCallFailed, DirectResponse):