Vizzuality / cartodbclient-php

A CartoDB client for PHP
http://developers.cartodb.com
BSD 4-Clause "Original" or "Old" License
33 stars 17 forks source link

runSql() exception incorrectly handles error message for CartoDB errors #6

Open kentr opened 11 years ago

kentr commented 11 years ago

The exception thrown by CartoDBClient::runSql() doesn't return a valid message string, at least when the error is a SQL error.

In that case, the CartoDB response is an array, which runSql() tries to concatenate onto the Exception message.

Suggest testing for HTTP response 400 and adjusting the error message accordingly.

Will follow with patch, rolled against HEAD, relative to cartodbclient-php directory.

kentr commented 11 years ago

Patch:

diff --git a/cartodb.class.php b/cartodb.class.php
index a970857..7a87457 100644
--- a/cartodb.class.php
+++ b/cartodb.class.php
@@ -102,8 +102,17 @@ class CartoDBClient {
     $params = array('q' => $sql);
     $response = $this->request('sql', 'POST', array('params' => $params));

-    if ($response['info']['http_code'] != 200) {
-      throw new Exception('There was a problem with your request: ' . $response['return']);
+    if ($response['info']['http_code'] == 400) {
+      throw new Exception(
+        'There was a problem with your request: ' . $response['return']['error'][0],
+        $response['info']['http_code']
+      );
+    }
+    elseif ($response['info']['http_code'] != 200) {
+      throw new Exception(
+       'There was a problem with your request: ' . $response['return'],
+        $response['info']['http_code']
+      );
     }
     return $response;
   }