On Fri, Jan 5, 2018 at 1:45 PM, Stephan Doliov <stephan.dol...@gmail.com>
wrote:

> Hi,
> I am diving ever deeper into Django and I came upon some behavior that
> frustrated me but perhaps I am just misunderstanding what should happen and
> why.
>
> In writing unit tests for code, I wanted to write one that traps a
> json.decoder.JSONDecodeError by passing in some malformed JSON.
>
> What appears to happen however, is that Django only reports back the very
> last exception that was raised.
>
> In the case of json.decoder.JSONDecodeError, it's initialization raises a
> ValueError here is the code of json.decoder.JSONDecodeError (from my python
> 3.5 install)
>
> class JSONDecodeError(ValueError):
>     """Subclass of ValueError with the following additional properties:
>
>
>     msg: The unformatted error message
>     doc: The JSON document being parsed
>     pos: The start index of doc where parsing failed
>     lineno: The line corresponding to pos
>     colno: The column corresponding to pos
>
>
>     """
>     # Note that this exception is used from _json
>     def __init__(self, msg, doc, pos):
>         lineno = doc.count('\n', 0, pos) + 1
>         colno = pos - doc.rfind('\n', 0, pos)
>         errmsg = '%s: line %d column %d (char %d)' % (msg, lineno, colno,
> pos)
>         ValueError.__init__(self, errmsg)
>         self.msg = msg
>         self.doc = doc
>         self.pos = pos
>         self.lineno = lineno
>         self.colno = colno
>
>
>     def __reduce__(self):
>         return self.__class__, (self.msg, self.doc, self.pos)
>
>
>
> Once my django parsing code (default django rest framework 3.7.7) steps
> into here, it's own ParseError class sees only the ValueError (which
> redacts the faulty input JSON).
>

This is it, the exception is catch by DRF


>
> Is this a feature? A bug? Something in between?
>
> My desired behavior would be that i could access the JSONDecodeError
> instances doc property to go back and find out exactly which malformed JSON
> was ruining my ability to serve a complete request.
>
> Is there a way I can get Django to make visible the underlying
> JSONDecodeError (that drove a Value Error that django rest framework then
> presents to me wrapped in its ParseError class)?
>
> Clearly, I could change the underlying JSON Decoder code to my liking,
> either by changing it's errmsg or by subclassing (ValueError) it in a way
> to provide more rich information back upstream?  If I run some sample code
> just in python CLI (less django) I get the whole JSONDecodeError exception
> as I desire. So I suspect modding the decoder class is not really what I
> want. But it would be nice if much like a stack trace, I could access
> ParseError instantianted by ValueError instantiated by JSONDecodeError so
> that I could access all the properties of the underlying error
> (JSONDecodeError).  Should I make a feature request? Or is there already a
> toggle I can control somewhere to do this?
>

Refer to the DRF code and see if you can modify it. If you want to make a
feature request, you will have to do it in DRF


>
> Thanks,
> Steve
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/15c7b76c-8dcd-4424-b336-8d0325c3a1e1%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/15c7b76c-8dcd-4424-b336-8d0325c3a1e1%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2BFDnh%2BYRbYtO2OzYatf4r3dN8Ug%3DdRLzGUyz6QfhreoLVfdAg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to