[utils] Handle JSON decode error in ckan_post
authorDenis Laxalde <denis.laxalde@logilab.fr>
Mon, 24 Nov 2014 15:49:18 +0100
changeset 19 d88cc9dbf6d6
parent 18 0becfca6d171
child 20 e7ff8cdf0adf
[utils] Handle JSON decode error in ckan_post .json() may fail, see http://docs.python-requests.org/en/latest/user/quickstart/#json-response-content.
utils.py
--- a/utils.py	Mon Nov 24 15:48:12 2014 +0100
+++ b/utils.py	Mon Nov 24 15:49:18 2014 +0100
@@ -34,11 +34,17 @@
     url = urljoin(base, 'api/3/action/' + action)
     headers = {'Authorization': config['ckan-api-key'],
                'Content-Type': 'application/json'}
-    r = requests.post(url, headers=headers, data=json.dumps(data or {}))
-    if not r.ok:
-        error = r.json()['error']
-        raise CKANPostError('action %s failed: %s' % (action, error))
-    return r.json()['result']
+    resp = requests.post(url, headers=headers, data=json.dumps(data or {}))
+    try:
+        jresp = resp.json()
+    except ValueError:
+        error = resp.text
+    else:
+        if resp.ok:
+            return jresp['result']
+        else:
+            error = jresp['error']
+    raise CKANPostError('action %s failed: %s' % (action, error))
 
 
 @objectify_predicate