title: Handling exceptions on a Pool of Requests
body: <p>I'm using Guzzle to send a number of requests to an API endpoint, using the <code>Pool</code> functionality to send these asynchronously and concurrently.</p>
  
  <p>The script looks like this:</p>
  
  <pre><code>use GuzzleHttp\Client;
  use GuzzleHttp\Promise;
  use GuzzleHttp\Pool;
  use GuzzleHttp\Psr7\Request;
  use GuzzleHttp\Exception\RequestException;
  
  /* Configure logger */
  Logger::configure("config/logger.xml");
  $logger = Logger::getLogger("console");
  
  /* Configure Guzzle Client */
  $client = new Client([
      'base_uri' =&gt; 'http://my.api/',
      'timeout' =&gt; 2.0,
      'allow_redirects' =&gt; false,
  ]);
  
  /* Anonymous function (closure) to 'yield' X number of Requests */
  $requests = function ($num_requests) {
      for ($i = 0; $i &lt; $num_requests; $i++) {
          yield new Request('GET', "/MY_UNIQUE_IDENTIFIER/");
      }
  };
  
  /* Create a Pool for the above requests */
  $pool = new Pool($client, $requests(20), [
      'concurrency' =&gt; 5,  // Determine how many requests to send concurrently
      'fulfilled' =&gt; function ($response, $index) {
          $logger-&gt;info('$index: ' . $index . ', $response: ' . $response);
      },
      'rejected' =&gt; function ($reason, $index) {
          try {
              echo $reason;
          } catch (\Exception $e) {
              trigger_error($e-&gt;getMessage(), E_USER_WARNING);
          }
      },
  ]);
  
  /* Initiate transfers/create a promise */
  $promise = $pool-&gt;promise();
  
  /* Force the pool of requests to complete */
  $promise-&gt;wait();
  </code></pre>
  
  <p>Basically, send 20 requests (5 at a time) to <code>http://my.api/MY_UNIQUE_IDENTIFIER</code>.</p>
  
  <p>The <code>Pool</code> appears to work. If I add an <code>echo</code> to the <code>rejected</code> requests, I get output like:</p>
  
  <pre><code>#0 /Users/me/guzzle-POC/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php(149): GuzzleHttp\Handler\CurlFactory::createRejection(Object(GuzzleHttp\Handler\EasyHandle), Array)
  #1 /Users/me/guzzle-POC/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php(102): GuzzleHttp\Handler\CurlFactory::finishError(Object(GuzzleHttp\Handler\CurlMultiHandler), Object(GuzzleHttp\Handler\EasyHandle), Object(GuzzleHttp\Handler\CurlFactory))
  #2 /Users/me/guzzle-POC/vendor/guzzlehttp/guzzle/src/Handler/CurlMultiHandler.php(181): GuzzleHttp\Handler\CurlFactory::finish(Object(GuzzleHttp\Handler\CurlMultiHandler), Object(GuzzleHttp\Handler\EasyHandle), Object(GuzzleHttp\Handler\CurlFactory))
  #3 /Users/me/guzzle-POC/vendor/guzzlehttp/guzzle/src/Handler/CurlMultiHandler.php(110): GuzzleHttp\Handler\CurlMultiHandler-&gt;processMessages()
  #4 /Users/me/guzzle-POC/vendor/guzzlehttp/guzzle/src/Handler/CurlMultiHandler.php(125): GuzzleHttp\Handler\CurlMultiHandler-&gt;tick()
  #5 /Users/me/guzzle-POC/vendor/guzzlehttp/promises/src/Promise.php(246): GuzzleHttp\Handler\CurlMultiHandler-&gt;execute(true)
  #6 /Users/me/guzzle-POC/vendor/guzzlehttp/promises/src/Promise.php(223): GuzzleHttp\Promise\Promise-&gt;invokeWaitFn()
  #7 /Users/me/guzzle-POC/vendor/guzzlehttp/promises/src/Promise.php(267): GuzzleHttp\Promise\Promise-&gt;waitIfPending()
  #8 /Users/me/guzzle-POC/vendor/guzzlehttp/promises/src/Promise.php(225): GuzzleHttp\Promise\Promise-&gt;invokeWaitList()
  #9 /Users/me/guzzle-POC/vendor/guzzlehttp/promises/src/Promise.php(62): GuzzleHttp\Promise\Promise-&gt;waitIfPending()
  #10 /Users/me/guzzle-POC/vendor/guzzlehttp/promises/src/EachPromise.php(101): GuzzleHttp\Promise\Promise-&gt;wait()
  #11 /Users/me/guzzle-POC/vendor/guzzlehttp/promises/src/Promise.php(246): GuzzleHttp\Promise\EachPromise-&gt;GuzzleHttp\Promise\{closure}(true)
  #12 /Users/me/guzzle-POC/vendor/guzzlehttp/promises/src/Promise.php(223): GuzzleHttp\Promise\Promise-&gt;invokeWaitFn()
  #13 /Users/me/guzzle-POC/vendor/guzzlehttp/promises/src/Promise.php(62): GuzzleHttp\Promise\Promise-&gt;waitIfPending()
  #14 /Users/me/guzzle-POC/poc.php(50): GuzzleHttp\Promise\Promise-&gt;wait()
  #15 {main}GuzzleHttp\Exception\ConnectException: cURL error 6: Could not resolve host: my.api (see http://curl.haxx.se/libcurl/c/libcurl-errors.html) in /Users/me/guzzle-POC/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php:185
  </code></pre>
  
  <p>The main problem here being <code>#15</code>, <code>Could not resolve host: my.api</code>. This is expected behaviour, but <strong>I want to catch this exception</strong>.</p>
  
  <p>The <code>try/catch</code> that I've used just doesn't work at all - it catches nothing.</p>
  
  <p>Presumably this is because of the Pool's asynchronous nature, but is it possible to catch these exceptions in any way?</p>
  
  <p>All I want to achieve basically is, <code>if can't resolve; log error and continue with other requests</code>-type approach.</p>