Projet

Général

Profil

0001-jsonresponse-use-string-conversion-on-exceptions-fix.patch

Benjamin Dauvergne, 12 janvier 2019 13:51

Télécharger (1,88 ko)

Voir les différences:

Subject: [PATCH] jsonresponse: use string conversion on exceptions (fixes
 #29060)

- introduce exception_to_text():
 - first try to use unicode/str on exception
 - then try to use repr,
 - finally only render class name and e.args (if present)
 passerelle/utils/jsonresponse.py | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
passerelle/utils/jsonresponse.py
2 2
# django-jsonresponse (https://github.com/jjay/django-jsonresponse) distributed
3 3
# under BSD license
4 4

  
5
import six
5 6
import datetime
6 7
import json
7 8
import functools
......
33 34
        super(APIError, self).__init__(*args)
34 35

  
35 36

  
37
def exception_to_text(e):
38
    try:
39
        return six.text_type(e)
40
    except Exception:
41
        pass
42

  
43
    try:
44
        return six.text_type(repr(e))
45
    except Exception:
46
        pass
47

  
48
    try:
49
        content = six.text_type(repr(e.args)) if e.args != [] else ''
50
    except Exception:
51
        content = '<exception>'
52
    return u'%s(%s)' % (e.__class__.__name__, content)
53

  
54

  
36 55
class JSONEncoder(DjangoJSONEncoder):
37 56
    def default(self, o):
38 57
        if isinstance(o, time.struct_time):
......
125 144
            data = self.obj_to_response(req, resp)
126 145
            status = 200
127 146
        except Exception as e:
128
            extras = {'method': req.method, 'exception': repr(e)}
147
            extras = {'method': req.method, 'exception': exception_to_text(e)}
129 148
            if not self.logger:
130 149
                extras['request'] = req
131 150
            if req.method == 'POST':
132
-