/** * Class constructor. It takes a raw response in JSON as parameter and * assembles itself from it. An exception will be thrown if the response * contains invalid JSON, if the JSON-RPC object represents an error or if * the JSON-RPC response is invalid. * @param string $json the response data * @throws SimpleJsonRpcClient\Exception\BaseException */ public function __construct($json, $skipErrorException = false) { $this->_rawResponse = $json; $response = $this->decode($json); // Check for mandatory fields foreach (array('jsonrpc', 'id') as $attribute) { if (!isset($response->{$attribute})) { throw new Exception\InvalidResponseException('Invalid JSON-RPC response. The raw response was: ' . $json); } $this->{$attribute} = $response->{$attribute}; } if ($response->jsonrpc !== '2.0') { throw new Exception\InvalidResponseException('Invalid JSON-RPC response. This client only supports JSON-RPC 2.0'); } if (isset($response->error)) { $this->error = new Error(json_encode($response->error)); // Optionally throw exception if (!$skipErrorException) { throw Exception\ResponseErrorFactory::create($this->error); } } else { $this->result = $response->result; } }
/** * @dataProvider errorProvider */ public function testCreate($errorJson) { $error = new \SimpleJsonRpcClient\Response\Error($errorJson); $exception = \SimpleJsonRpcClient\Exception\ResponseErrorFactory::create($error); $this->assertEquals(get_class($exception), $error->message); }