guzzle / guzzle

Guzzle, an extensible PHP HTTP client
https://docs.guzzlephp.org/
MIT License
23.17k stars 2.41k forks source link

How do you catch exceptions in pool? #1694

Closed AdamKyle closed 4 years ago

AdamKyle commented 7 years ago

Consider the following example:

    protected function getOptions($howManyItemsBack, $successCallbackFunction, $rejectedCallbackFunction) {
        return [
            'concurrency' => 18,
            'fulfilled'   => function ($response, $index) use (&$howManyItemsBack, &$successCallbackFunction) {

                $responseJson         = json_decode($response->getBody()->getContents());
                $responseJson->items  = array_slice($responseJson->items, $howManyItemsBack);

                call_user_func_array($successCallbackFunction, array($this->regionAndItemPairs[$index], $responseJson));
            },
            'rejected'    => function ($reason, $index) use (&$rejectedCallbackFunction)  {
                call_user_func_array($rejectedCallbackFunction, array($reason, $index));
            },
        ];
    }

The rejected is what I care about here. essentially at the end of the day all I do with the rejected section once the reason and index are passed back to the callback function is log it out, so you see things like this:

[2016-12-18 05:26:59] production.INFO: Errors caught when trying to fetch:  ["[object] (GuzzleHttp\\Exception\\ClientException(code: 404): Client error: `GET https://crest-tq.eveonline.com/market/10000002/history/?type=https://crest-tq.eveonline.com/inventory/types/32775/` resulted in a `404 NOT FOUND` response:\n{\"message\": \"Type not listed on market\", \"key\": \"typeNotOnMarket\", \"exceptionType\": \"NotFoundError\"}\n at /home/xxx/personal_site/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:111)"] []

The key here is that this is throwing a ClientException (404)

So my question to you, is how do I do a try catch or get this error when working with pooled requests? How do you catch any error when working with pooled requests?

Your docs have try/catch for basic client requests but nothing for pooled.

sagikazarmark commented 7 years ago

Not sure I understand your question, but I will try to answer it.

The first confusing thing about your question is "pooled requests". The Pool itself works asynchronously, which means you receive a Promise where can define success and rejection callbacks. This should allow you to handle rejections.

When you call Pool::batch then the theory is the same, except that the Promise is automatically resolved for you and the return value is an array, containing your responses AND exceptions in the order of requests. Even in this case you can define the rejection callback which should be called, so it's up to you if you want to handle the error with a callback or you process the returned response/exception array.

So the short answers is that you cannot handle exceptions as you would normally, since in this case there isn't a linear flow, but a "parallel" one. Which exception should be thrown if you have 3 at the same time?

Hope this helps!

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 2 weeks if no further activity occurs. Thank you for your contributions.