From 48778c4dbcde1756b2e4a374329d213fe1ab9de1 Mon Sep 17 00:00:00 2001 From: netkas Date: Thu, 3 Apr 2025 17:48:03 -0400 Subject: [PATCH] Enhance RPC client logging and error handling for server information retrieval --- src/Socialbox/Classes/RpcClient.php | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/Socialbox/Classes/RpcClient.php b/src/Socialbox/Classes/RpcClient.php index 478b4c1..851c385 100644 --- a/src/Socialbox/Classes/RpcClient.php +++ b/src/Socialbox/Classes/RpcClient.php @@ -414,10 +414,12 @@ curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POSTFIELDS, $encryptedData); - $this->logger->debug(sprintf('Sending RPC request to %s', $this->rpcEndpoint)); - $this->logger->debug(sprintf('Headers: %s', json_encode($headers))); - $this->logger->debug(sprintf('Encrypted Data Size: %d', strlen($encryptedData))); + $this->logger->debug(sprintf('Request Endpoint: %s', $this->rpcEndpoint)); + $this->logger->debug(sprintf('Request Headers: %s', json_encode($headers))); + $this->logger->debug(sprintf('Request Size: %d bytes', strlen($encryptedData))); $this->logger->debug(sprintf('Request Signature: %s', $signature)); + $this->logger->debug(sprintf('Request Data: %s', $jsonData)); + $response = curl_exec($ch); @@ -454,7 +456,7 @@ } curl_close($ch); - $this->logger->debug(sprintf('Encrypted Response Size: %d', strlen($responseString))); + $this->logger->debug(sprintf('Response Size: %d bytes', strlen($responseString))); try { @@ -463,7 +465,7 @@ encryptionKey: $this->clientTransportEncryptionKey, algorithm: $this->serverInformation->getTransportEncryptionAlgorithm() ); - $this->logger->debug(sprintf('Decrypted Response: %s', $decryptedResponse)); + $this->logger->debug(sprintf('Response Data: %s', $decryptedResponse)); } catch (CryptographyException $e) { @@ -532,14 +534,14 @@ $this->logger->debug(sprintf('Getting server information from %s', $this->rpcEndpoint)); $this->logger->debug(sprintf('Headers: %s', json_encode($headers))); $response = curl_exec($ch); + $responseCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE); if($response === false) { curl_close($ch); - throw new RpcException(sprintf('Failed to get server information at %s, no response received', $this->rpcEndpoint)); + throw new RpcException(sprintf('Failed to get server information at %s with response code %d, no response received', $this->rpcEndpoint, $responseCodet)); } - $responseCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE); if($responseCode !== 200) { curl_close($ch); @@ -551,7 +553,18 @@ } curl_close($ch); - return ServerInformation::fromArray(json_decode($response, true)); + if(empty($response)) + { + throw new RpcException(sprintf('Failed to get server information at %s with response code %d, server returned an empty response', $this->rpcEndpoint, $responseCode)); + } + + $decodedResponse = json_decode($response, true); + if(!is_array($decodedResponse)) + { + throw new RpcException(sprintf('Failed to get server information at %s with response code %d, server did not return a valid response', $this->rpcEndpoint, $responseCode)); + } + + return ServerInformation::fromArray($decodedResponse); } /**