Handle JSON decode errors in API requests, throw synthetic error.
authorChris Burgess <chris@giantrobot.co.nz>
Wed, 4 Sep 2013 22:24:50 +0000 (10:24 +1200)
committerChris Burgess <chris@giantrobot.co.nz>
Wed, 4 Sep 2013 22:24:50 +0000 (10:24 +1200)
api/class.api.php

index f6c9f500b4a8c6b70621e8eae04b7944776a8bb2..5e27abc454ef33f2094cfdb3dfb91ad5dd123d50 100644 (file)
@@ -179,23 +179,21 @@ class civicrm_api3 {
         return $res;
       }
       curl_close($ch);
-      $res = json_decode($result);
-      if (!$res) {
-        $res = new stdClass;
-        $res->is_error = 1;
-        $res->error_message = "not a valid json returned by the server";
-        $res->level = "json_decode";
-        $res->row_result = $result;
-        
-      }
-      return $res;
     }
     else {
       // Should be discouraged, because the API credentials and data
       // are submitted as GET data, increasing chance of exposure..
       $result = file_get_contents($query . '&' . $fields);
-      return json_decode($result);
     }
+    if (!$res = json_decode($result)) {
+      $res = new stdClass;
+      $res->is_error = 1;
+      $res->error_message = 'Unable to parse returned JSON';
+      $res->level = 'json_decode';
+      $res->error = array('Unable to parse returned JSON' => $result);
+      $res->row_result = $result;
+    }
+    return $res;
   }
 
   function call($entity, $action = 'Get', $params = array()) {