datto / php-json-rpc

Fully unit-tested JSON-RPC 2.0 for PHP
GNU Lesser General Public License v3.0
185 stars 39 forks source link

ErrorException "Invalid JSON-RPC 2.0 response" thrown when decoding message with error object #11

Closed raraworks closed 5 years ago

raraworks commented 5 years ago

I might be incorrect, but it seems that when decoding response from JSON RPC server, and response contains error object, parameter result is requested where as per specification it is not required:

image

And so decode fails and throws an errorException.

smortensen commented 5 years ago

It turns out we use the existence of the 'result' property to decide whether we're dealing with a result object or an error object.

Here's the corresponding line of code: https://github.com/datto/php-json-rpc/blob/4465221e4d40ffb7fdfd72a0bd86eb9a3b7a47df/src/Client.php#L213-L214

So! If there is no 'result' property, then the "getResult" function will return false and we'll end up fetching an error object instead.

raraworks commented 5 years ago

Yes, this was correct.

The API I was connecting to, was not sending correct error object structure (error code was not an integer).

You are completely correct, thanks for clarification.