Commit 37fe07e9 authored by Denis Laxalde's avatar Denis Laxalde
Browse files

[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).
parent 3a1f742b84cd
......@@ -1012,6 +1012,13 @@ class AjaxControllerTC(CubicWebTC):
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
......
......@@ -67,6 +67,7 @@ from warnings import warn
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 @@ class AjaxController(Controller):
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 @@ class AjaxController(Controller):
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 @@ class AjaxController(Controller):
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):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment