/** * {@inheritdoc} */ public function flush() { if (0 === $this->queue->count()) { return 0; } $queue = clone $this->queue; $this->queue->clear(); try { $responses = $this->httpAdapter->sendRequests($queue->all()); } catch (MultiHttpAdapterException $e) { // Handle all networking errors: php-http only throws an exception // if no response is available. $collection = new ExceptionCollection(); foreach ($e->getExceptions() as $exception) { // php-http only throws an exception if no response is available if (!$exception->getResponse()) { // Assume networking error if no response was returned. $collection->add(ProxyUnreachableException::proxyUnreachable($exception)); } } foreach ($this->handleErrorResponses($e->getResponses()) as $exception) { $collection->add($exception); } throw $collection; } $exceptions = $this->handleErrorResponses($responses); if (count($exceptions) > 0) { throw new ExceptionCollection($exceptions); } return count($queue); }
public function testCollection() { $collection = new ExceptionCollection(); $this->assertNull($collection->getFirst()); $e1 = new \RuntimeException(); $collection->add($e1); $e2 = new \RuntimeException('Message'); $collection->add($e2); $this->assertEquals('Message', $collection->getMessage()); $this->assertCount(2, $collection); $this->assertSame($e1, $collection->getFirst()); $actual = array(); foreach ($collection as $e) { $actual[] = $e; } $this->assertEquals(array($e1, $e2), $actual); }
/** * Handle request exception * * @param GuzzleExceptionCollection $exceptions * * @throws ExceptionCollection */ protected function handleException(GuzzleExceptionCollection $exceptions) { $collection = new ExceptionCollection(); foreach ($exceptions as $exception) { if ($exception instanceof CurlException) { // Caching proxy unreachable $e = ProxyUnreachableException::proxyUnreachable( $exception->getRequest()->getHost(), $exception->getMessage(), $exception->getRequest()->getRawHeaders(), $exception ); } elseif ($exception instanceof RequestException) { // Other error $e = ProxyResponseException::proxyResponse( $exception->getRequest()->getHost(), $exception->getCode(), $exception->getMessage(), $exception->getRequest()->getRawHeaders(), $exception ); } else { // Unexpected exception type $e = $exception; } $collection->add($e); } throw $collection; }
/** * @expectedException \FOS\HttpCache\Exception\ExceptionCollection */ public function testProxyClientExceptionsAreLogged() { $unreachableException = ProxyUnreachableException::proxyUnreachable('http://127.0.0.1', 'Couldn\'t connect to host'); $responseException = ProxyResponseException::proxyResponse('http://127.0.0.1', 403, 'Forbidden'); $exceptions = new ExceptionCollection(); $exceptions->add($unreachableException)->add($responseException); $proxyClient = \Mockery::mock('\FOS\HttpCache\ProxyClient\ProxyClientInterface') ->shouldReceive('flush')->once()->andThrow($exceptions) ->getMock(); $cacheInvalidator = new CacheInvalidator($proxyClient); $logger = \Mockery::mock('\Psr\Log\LoggerInterface') ->shouldReceive('log')->once() ->with( 'critical', 'Request to caching proxy at http://127.0.0.1 failed with message "Couldn\'t connect to host"', array( 'exception' => $unreachableException ) ) ->shouldReceive('log')->once() ->with('critical', '403 error response "Forbidden" from caching proxy at http://127.0.0.1', array('exception' => $responseException)) ->getMock(); $cacheInvalidator->addSubscriber(new LogSubscriber($logger)); $cacheInvalidator ->flush() ; }