[web/ajax] Always return a json dict with a 'reason' key in case of ajax errors

Sending sometimes a bare unicode string and sometimes a json-encoded dict seems like a bad idea.

authorJulien Cristau <julien.cristau@logilab.fr>
changesetf79ce7b39ee9
branchdefault
phasepublic
hiddenno
parent revision#33f6ad4c75a5 [web/ajax] don't override any status code with 500 in ajax_error_handler
child revision#691cbfc89f83 [web] Fix HEAD request handling (closes #3677949), #8347e6d613c9 [web/test] add a test for some http response codes
files modified by this revision
web/application.py
# HG changeset patch
# User Julien Cristau <julien.cristau@logilab.fr>
# Date 1394726595 -3600
# Thu Mar 13 17:03:15 2014 +0100
# Node ID f79ce7b39ee9e4cebb9a3eb7bef1e465c3de566a
# Parent 33f6ad4c75a5bf07cf27c583be96bb4e5f0d3a05
[web/ajax] Always return a json dict with a 'reason' key in case of ajax errors

Sending sometimes a bare unicode string and sometimes a json-encoded
dict seems like a bad idea.

diff --git a/web/application.py b/web/application.py
@@ -21,10 +21,11 @@
1 
2  import sys
3  from time import clock, time
4  from contextlib import contextmanager
5  from warnings import warn
6 +import json
7 
8  import httplib
9 
10  from logilab.common.deprecation import deprecated
11 
@@ -582,11 +583,11 @@
12          if isinstance(ex, PublishException) and ex.status is not None:
13              status = ex.status
14          if req.status_out < 400:
15              # don't overwrite it if it's already set
16              req.status_out = status
17 -        json_dumper = getattr(ex, 'dumps', lambda : unicode(ex))
18 +        json_dumper = getattr(ex, 'dumps', lambda : json.dumps({'reason': unicode(ex)}))
19          return json_dumper()
20 
21      # special case handling
22 
23      def need_login_content(self, req):