graze / guzzle-jsonrpc

JSON-RPC 2.0 client for Guzzle
https://packagist.org/packages/graze/guzzle-jsonrpc
MIT License
93 stars 61 forks source link

Throw exception on RPC error #15

Closed adlawson closed 10 years ago

adlawson commented 10 years ago

RPC errors are reported differently to HTTP errors and weren't previously handled automatically. The subscriber in this PR throws an exception on error if it's added (not default to avoid breaking BC).

Enabling the subscriber is shown in the docs as such:

<?php
use Graze\GuzzleHttp\JsonRpc\Client;
use Graze\GuzzleHttp\JsonRpc\Exception\RequestException;
use Graze\GuzzleHttp\JsonRpc\Subscriber\ErrorSubscriber;

// Create the client
$client = Client::factory('http://localhost:8000');

// Create a request and attach the error subscriber
$request = $client->request(123, 'method', ['key'=>'value']);
$request->getEmitter()->addSubscriber(new ErrorSubscriber());

// Send the request
try {
    $client->send($request);
} catch (RequestException $e) {
    die($e->getResponse()->getRpcErrorMessage());
}