classification
Title: Json error messages could provide more information about the error
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: ezio.melotti Nosy List: Luka.Rahne, asvetlov, bob.ippolito, eric.araujo, ezio.melotti, maker, python-dev, r.david.murray, serhiy.storchaka
Priority: normal Keywords: needs review, patch

Created on 2012-09-23 17:14 by Luka.Rahne, last changed 2013-01-03 21:53 by ezio.melotti. This issue is now closed.

Files
File name Uploaded Description Edit
issue16009.patch maker, 2012-10-04 14:35 review
json_errmsg_4.patch serhiy.storchaka, 2012-11-01 21:42 review
json_errmsg_5.patch serhiy.storchaka, 2012-11-09 15:56 review
Messages (14)
msg171062 - (view) Author: Luka Rahne (Luka.Rahne) Date: 2012-09-23 17:14
Example:

import json
j = json.loads(
"""
{
    "phoneNumber": [
        {
            "type": "home",
            "number": "212 555-1234"
        },
}

""")
print(j)

Output>>
Traceback (most recent call last):
  File "<stdin>", line 13, in <module>
  File "/usr/lib/python2.7/json/__init__.py", line 326, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 384, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded


My comment:
It is annoying in case of manual json editing where you can't know where jeson typeing error is.
msg171064 - (view) Author: Luka Rahne (Luka.Rahne) Date: 2012-09-23 17:21
For example i foun out, that just extra comma crate such error

j = json.loads("""{ "" :[{},]}""")
msg171072 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-09-23 18:10
The errors are being handled correctly, but it would indeed be nice for them to provide more information about the specific error.  That's an enhancement, though.
msg171927 - (view) Author: Michele OrrĂ¹ (maker) * Date: 2012-10-04 09:09
Since AFAIK json.loads runs through every simil-str object, I think that a repr() is fine here. What do you think?
msg171943 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-10-04 13:31
A repr of what?
msg171955 - (view) Author: Michele OrrĂ¹ (maker) * Date: 2012-10-04 14:30
> A repr of what?
Of the variable "s" in Lib/json/decoder.py#l358
msg171956 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-10-04 14:45
So you would see a copy of the string being decoded in the error message?  That string might be very long, and would be of doubtful utility.  What I had in mind was at least printing the character position of the parse failure.  That may also be of limited utility, though, since in many cases the parse failure will happen at the end of the string.  This is a problem for all parsers, and the solution is often non-trivial, depending on the parser.  

Someone who wants to see this happen will need to look at the parser and see what information it has at the point of parse failure that might be useful and practical to expose.
msg171974 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-10-04 16:48
Here is a patch that makes error message more informative. I also changed message "Expecting object" to "Expecting value" as more conformed to JSON terminology.
msg171989 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-10-04 19:28
Patch updated. Conformed other error messages. Now expected exceptions raised for all of the following strings:

''
'['
'[42'
'[42,'
'{'
'{"'
'{"spam'
'{"spam"'
'{"spam":'
'{"spam":42'
'{"spam":42,'
msg172086 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-10-05 14:15
Patch updated. Tests added. One discrepancy between Python an C versions fixed.

In fact, we can get rid of StopIteration and directly raise ValueError. I don't know what are the functions of StopIteration here, so preserve it. Perhaps the author could explain the details.
msg174480 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-11-01 21:42
Patch updated. Fixed a debug artifact and a deprecation warning.
msg175244 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-11-09 15:56
Patch updated.  Added new test cases as Ezio want..
msg178914 - (view) Author: Roundup Robot (python-dev) Date: 2013-01-03 06:44
New changeset 17b4bf9e9f43 by Ezio Melotti in branch 'default':
#16009: JSON error messages now provide more information.  Patch by Serhiy Storchaka.
http://hg.python.org/cpython/rev/17b4bf9e9f43
msg178915 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-01-03 06:45
Fixed, thanks for the patch!
History
Date User Action Args
2014-05-07 17:55:44r.david.murraylinkissue21451 superseder
2013-01-03 21:53:20ezio.melottisetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2013-01-03 21:44:04ezio.melottisetstatus: closed -> open
assignee: ezio.melotti
resolution: fixed -> (no value)
stage: resolved -> patch review
2013-01-03 06:45:15ezio.melottisetstatus: open -> closed
messages: + msg178915

assignee: ezio.melotti -> (no value)
resolution: fixed
stage: patch review -> resolved
2013-01-03 06:44:27python-devsetnosy: + python-dev
messages: + msg178914
2012-11-26 20:05:16ezio.melottisetassignee: ezio.melotti
2012-11-09 15:56:39serhiy.storchakasetfiles: + json_errmsg_5.patch

messages: + msg175244
2012-11-01 21:42:16serhiy.storchakasetfiles: + json_errmsg_4.patch

messages: + msg174480
2012-11-01 21:40:44serhiy.storchakasetfiles: - json_errmsg_3.patch
2012-11-01 21:32:26serhiy.storchakasetfiles: - json_errmsg_2.patch
2012-11-01 21:31:57serhiy.storchakasetfiles: - json_errmsg.patch
2012-10-07 18:58:05asvetlovsetnosy: + asvetlov
2012-10-05 14:15:15serhiy.storchakasetfiles: + json_errmsg_3.patch

nosy: + bob.ippolito
messages: + msg172086

keywords: + needs review
stage: test needed -> patch review
2012-10-05 00:38:10ezio.melottisetstage: needs patch -> test needed
2012-10-04 19:28:34serhiy.storchakasetfiles: + json_errmsg_2.patch

messages: + msg171989
2012-10-04 18:18:12serhiy.storchakasetfiles: + json_errmsg.patch
2012-10-04 18:14:44serhiy.storchakasetfiles: - json_errmsg.patch
2012-10-04 16:48:55serhiy.storchakasetfiles: + json_errmsg.patch
nosy: + serhiy.storchaka
messages: + msg171974

2012-10-04 14:45:30r.david.murraysetmessages: + msg171956
2012-10-04 14:35:03makersetfiles: + issue16009.patch
keywords: + patch
2012-10-04 14:30:24makersetmessages: + msg171955
2012-10-04 13:31:40r.david.murraysetmessages: + msg171943
2012-10-04 09:09:14makersetnosy: + maker
messages: + msg171927
2012-09-30 17:03:03ezio.melottisetnosy: + ezio.melotti
2012-09-24 22:58:35eric.araujosetnosy: + eric.araujo
2012-09-23 18:10:07r.david.murraysetversions: + Python 3.4, - Python 2.7, Python 3.2
type: enhancement

nosy: + r.david.murray
title: Json not handling errors correctly -> Json error messages could provide more information about the error
messages: + msg171072
stage: needs patch
2012-09-23 17:21:49Luka.Rahnesetmessages: + msg171064
2012-09-23 17:14:25Luka.Rahnecreate