diff --git a/.gitignore b/.gitignore index b67fc9b..678757c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,36 @@ vendor/ .project .settings .buildpath + + +### Intellij +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm +.idea/ + +### Numerous always-ignore extensions +*.diff +*.err +*.orig +*.ods +*.ods# +*.log +*.rej +*.swo +*.swp +*.zip +*.vi +*~ + +### OS or Editor folders +.DS_Store +._* +Thumbs.db +desktop.ini +.cache +.project +.settings +.tmproj +*.esproj +nbproject +*.sublime-project +*.sublime-workspace diff --git a/src/org/jsonrpcphp/JsonRPCClient.php b/src/org/jsonrpcphp/JsonRPCClient.php index f5d5349..d1548f3 100644 --- a/src/org/jsonrpcphp/JsonRPCClient.php +++ b/src/org/jsonrpcphp/JsonRPCClient.php @@ -28,6 +28,7 @@ * @author sergio * @author Johannes Weberhofer */ + namespace org\jsonrpcphp; class JsonRPCClient @@ -87,7 +88,7 @@ public function __construct($url, $debug = false, $proxy = null) { $this->url = $url; $this->proxy = $proxy; - $this->debug = ($this->debug === true); + $this->debug = $debug; // message id $this->id = 1; } @@ -108,13 +109,15 @@ public function setRPCNotification($notification) * * @param string $method * @param array $params + * * @return array + * @throws \Exception */ public function __call($method, $params) { // check - if (! is_scalar($method)) { + if ( ! is_scalar($method)) { throw new \Exception('Method name has no scalar value'); } @@ -137,11 +140,11 @@ public function __call($method, $params) $request = array( 'method' => $method, 'params' => $params, - 'id' => $currentId + 'id' => $currentId, ); $request = json_encode($request); if ($this->debug) { - echo '***** Request *****' . "\n" . $request . "\n"; + echo '***** Request *****'."\n".$request."\n"; } // performs the HTTP POST @@ -150,7 +153,7 @@ public function __call($method, $params) $ch = curl_init($this->url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array( - 'Content-type: application/json' + 'Content-type: application/json', )); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); @@ -160,40 +163,84 @@ public function __call($method, $params) } $response = curl_exec($ch); if ($response === false) { - throw new \Exception('Unable to connect to ' . $this->url); + throw new \Exception('Unable to connect to '.$this->url); } } else { $opts = array( 'http' => array( 'method' => 'POST', 'header' => 'Content-type: application/json', - 'content' => $request - ) + 'content' => $request, + ), ); $context = stream_context_create($opts); if ($fp = fopen($this->url, 'r', false, $context)) { $response = ''; while ($row = fgets($fp)) { - $response .= trim($row) . "\n"; + $response .= trim($row)."\n"; } } else { - throw new \Exception('Unable to connect to ' . $this->url); + throw new \Exception('Unable to connect to '.$this->url); } } if ($this->debug) { - echo '***** Response *****' . "\n" . $response . "\n" . '***** End of Response *****' . "\n\n"; + echo '***** Response *****'."\n".$response."\n".'***** End of Response *****'."\n\n"; } $response = json_decode($response, true); + // Check for errors + $jsonLastError = null; + switch (json_last_error()) { + case JSON_ERROR_NONE: + break; + case JSON_ERROR_DEPTH: + $jsonLastError = ' - Maximum stack depth exceeded'; + break; + case JSON_ERROR_STATE_MISMATCH: + $jsonLastError = ' - Underflow or the modes mismatch'; + break; + case JSON_ERROR_CTRL_CHAR: + $jsonLastError = ' - Unexpected control character found'; + break; + case JSON_ERROR_SYNTAX: + $jsonLastError = ' - Syntax error, malformed JSON'; + break; + case JSON_ERROR_UTF8: + $jsonLastError = ' - Malformed UTF-8 characters, possibly incorrectly encoded'; + break; + default: + $jsonLastError = ' - Unknown error'; + break; + } + // final checks and return - if (! $this->notification) { - // check - if ($response['id'] != $currentId) { - throw new \Exception('Incorrect response id: ' . $response['id'] . ' (request id: ' . $currentId . ')'); + if ( ! $this->notification) { + $errorMessage = null; + switch (true) { + case ! is_array($response): + $errorMessage = sprintf('Incorrect response: %s. (jsonLastError: %s)', $response, $jsonLastError); + break; + case ! isset($response['id']): + $errorMessage = sprintf('The response has no response id! (jsonLastError: %s || response: %s)', + $jsonLastError, json_encode($response)); + break; + case $response['id'] != $currentId: + $errorMessage = sprintf('Missmatching response id: recieved %s but was expecting %s . (response: %s)', + $response['id'], $currentId, json_encode($response)); + break; + case array_key_exists('error', $response) && $response['error']: + $errorMessage = sprintf('Request error: %s', json_encode($response['error'])); + break; + case ! array_key_exists('result', $response): + $errorMessage = sprintf('Request has no result to return! (response: %s)', json_encode($response)); + break; + + default: } - if (array_key_exists('error', $response) && $response['error'] !== null) { - throw new \Exception('Request error: ' . json_encode($response['error'])); + + if ($errorMessage) { + throw new \Exception($errorMessage); } return $response['result'];