Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #24508

json.loads() should return a more specific error

From Roy Smith <roy@panix.com>
Newsgroups comp.lang.python
Subject json.loads() should return a more specific error
Date 2012-06-27 08:45 -0400
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <roy-D283F0.08454827062012@news.panix.com> (permalink)

Show all headers | View raw


Before I go open an enhancement request, what do people think of the 
idea that json.load() should return something more specific than 
ValueError?

I've got some code that looks like

    try:
        response = requests.get(url)
    except RequestException as ex:
        logger.exception(ex)
        return []
    data = response.text
    try:
        events = json.loads(data)
    except ValueError as ex:
        logger.error("%s: %r", ex, data)
        return []

This would be so much neater if json would return something I could 
identify as a json error.  It would all just collapse into:

    try:
        events = requests.get(url).json
    except (RequestException, JSONDecodeError) as ex:
        logger.exception(ex)
        return []
 
We could make JSONDecodeError a subclass of ValueError so existing code 
would continue to work.

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

json.loads() should return a more specific error Roy Smith <roy@panix.com> - 2012-06-27 08:45 -0400
  Re: json.loads() should return a more specific error Terry Reedy <tjreedy@udel.edu> - 2012-06-27 13:57 -0400

csiph-web