From 92028e1730a2042a9b7cd7883305aa4fc578ed05 Mon Sep 17 00:00:00 2001 From: Abdessamad Idrissi Date: Mon, 21 Nov 2022 17:47:46 +0100 Subject: [PATCH 1/4] Fix debug always false Fix debug always false --- src/org/jsonrpcphp/JsonRPCClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/org/jsonrpcphp/JsonRPCClient.php b/src/org/jsonrpcphp/JsonRPCClient.php index f5d5349..23e64a8 100644 --- a/src/org/jsonrpcphp/JsonRPCClient.php +++ b/src/org/jsonrpcphp/JsonRPCClient.php @@ -87,7 +87,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; } From 454b9d5013c2562c8eaee661a495c98f26e3351f Mon Sep 17 00:00:00 2001 From: Almo Date: Mon, 21 Nov 2022 18:32:50 +0100 Subject: [PATCH 2/4] add more debugging messages to response errors --- .gitignore | 33 ++++++++++++ src/org/jsonrpcphp/JsonRPCClient.php | 81 ++++++++++++++++++++++------ 2 files changed, 97 insertions(+), 17 deletions(-) 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 23e64a8..6a270b8 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 @@ -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 (array_key_exists('error', $response) && $response['error'] !== null) { - throw new \Exception('Request error: ' . json_encode($response['error'])); + if ( ! $this->notification) { + switch (true) { + case ! is_array($response): + throw new \Exception( + sprintf('Incorrect response: %s. (jsonLastError: %s)', $response, $jsonLastError)); + break; + case ! isset($response['id']): + throw new \Exception( + sprintf('The response has no response id! (jsonLastError: %s || response: %s)', $jsonLastError, + json_encode($response))); + break; + case $response['id'] != $currentId: + throw new \Exception( + 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): + throw new \Exception( + sprintf('Request error: %s', json_encode($response['error']))); + break; + case ! array_key_exists('result', $response): + throw new \Exception( + sprintf('Request has no result to return! (response: %s)', json_encode($response))); + break; + + default: } return $response['result']; From 95fd6c1e1b4c7ac876fe9340a7805f62c7ea980d Mon Sep 17 00:00:00 2001 From: Almo Date: Mon, 21 Nov 2022 19:15:47 +0100 Subject: [PATCH 3/4] fix typo --- src/org/jsonrpcphp/JsonRPCClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/org/jsonrpcphp/JsonRPCClient.php b/src/org/jsonrpcphp/JsonRPCClient.php index 6a270b8..3c9d0c0 100644 --- a/src/org/jsonrpcphp/JsonRPCClient.php +++ b/src/org/jsonrpcphp/JsonRPCClient.php @@ -231,7 +231,7 @@ public function __call($method, $params) 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): + case array_key_exists('error', $response) && $response['error']: throw new \Exception( sprintf('Request error: %s', json_encode($response['error']))); break; From 172cdf52388e19087ed1a8b721f0641252fa5316 Mon Sep 17 00:00:00 2001 From: Almo Date: Mon, 21 Nov 2022 21:13:21 +0100 Subject: [PATCH 4/4] tidy up error message --- src/org/jsonrpcphp/JsonRPCClient.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/org/jsonrpcphp/JsonRPCClient.php b/src/org/jsonrpcphp/JsonRPCClient.php index 3c9d0c0..d1548f3 100644 --- a/src/org/jsonrpcphp/JsonRPCClient.php +++ b/src/org/jsonrpcphp/JsonRPCClient.php @@ -216,33 +216,33 @@ public function __call($method, $params) // final checks and return if ( ! $this->notification) { + $errorMessage = null; switch (true) { case ! is_array($response): - throw new \Exception( - sprintf('Incorrect response: %s. (jsonLastError: %s)', $response, $jsonLastError)); + $errorMessage = sprintf('Incorrect response: %s. (jsonLastError: %s)', $response, $jsonLastError); break; case ! isset($response['id']): - throw new \Exception( - sprintf('The response has no response id! (jsonLastError: %s || response: %s)', $jsonLastError, - json_encode($response))); + $errorMessage = sprintf('The response has no response id! (jsonLastError: %s || response: %s)', + $jsonLastError, json_encode($response)); break; case $response['id'] != $currentId: - throw new \Exception( - sprintf('Missmatching response id: recieved %s but was expecting %s . (response: %s)', - $response['id'], $currentId, json_encode($response))); + $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']: - throw new \Exception( - sprintf('Request error: %s', json_encode($response['error']))); + $errorMessage = sprintf('Request error: %s', json_encode($response['error'])); break; case ! array_key_exists('result', $response): - throw new \Exception( - sprintf('Request has no result to return! (response: %s)', json_encode($response))); + $errorMessage = sprintf('Request has no result to return! (response: %s)', json_encode($response)); break; default: } + if ($errorMessage) { + throw new \Exception($errorMessage); + } + return $response['result']; } else { return true;