Closed itsonit closed 8 years ago
There are a few options available.
Just catch the exception.
try
{
$response = $service->operation($request);
// Handle successful response.
}
catch(Exception $e)
{
/**
* Add strategy for dealing with the exception.
* For example log exception message or re-try request.
*/
}
You can also get access to the actual Guzzle3 object that sends the request. The httpClient method returns the HttpClient object that the SDK uses for sending the request. This object uses a Guzzle3 instance that can be obained via the guzzle method. This will enable you to use some of the Guzzle3 plugins that are available.
use GuzzleHttp\Subscriber\Retry\RetrySubscriber;
/**
* Use a plugin that retrys 500 and 503 responses.
*/
$retry = new RetrySubscriber([
'filter' => RetrySubscriber::createStatusFilter()
]);
$service = new Services\TradingService(...);
/**
* Attach pluging to Guzzle3 object.
*/
$service->httpClient()->guzzle()->getEmitter()->attach($retry);
$response = $service->operation($request);
It's also possible to supply the SDK with your own HTTP client and let that handle the actual request. You can then implement what ever retry mechanisam and error handling that you feel is suitable for your project. There is an example available that shows how to use cURL to send requests to the API.
Thank you for your detailed reply! (:
I had already tried to catch the exception globally, but that did not work.
I will try to solve it with Guzzle RetrySubscriber as you described.
Shame on me. I had just catched a CurlException, and no general Exception.. Sorry for the noise.
A 503 API call produce a fatal error:
`[17-Nov-2015 22:10:32 Europe/Berlin] PHP Fatal error: Uncaught exception 'Guzzle\Http\Exception\ServerErrorResponseException' with message 'Server error response [status code] 503 [reason phrase] Service Unavailable [url] https://api.ebay.com/ws/api.dll' in /etc/cudgelmiddleware/code/vendor/guzzle/guzzle/src/Guzzle/Http/Exception/BadResponseException.php:43 Stack trace:
0 /etc/cudgelmiddleware/code/vendor/guzzle/guzzle/src/Guzzle/Http/Message/Request.php(145): Guzzle\Http\Exception\BadResponseException::factory(Object(Guzzle\Http\Message\EntityEnclosingRequest), Object(Guzzle\Http\Message\Response))
1 [internal function]: Guzzle\Http\Message\Request::onRequestError(Object(Guzzle\Common\Event), 'request.error', Object(Symfony\Component\EventDispatcher\EventDispatcher))
2 /etc/cudgelmiddleware/code/vendor/symfony/event-dispatcher/EventDispatcher.php(158): call_user_func(Array, Object(Guzzle\Common\Event), 'request.error', Object(Symfony\Component\EventDispatcher\EventDispatcher))
3 /etc/cudgelmiddleware/code/vendor/symfony/event-dispatcher/EventDispatcher.php( in /etc/cudgelmiddleware/code/vendor/guzzle/guzzle/src/Guzzle/Http/Exception/BadResponseException.php on line 43`
Best practise to catch this? ty very much!