public function testAddsNonRequestExceptions() { $e = new MultiTransferException(); $e->add(new \Exception('bar')); $e->addFailedRequestWithException(new Request('GET', 'http://www.foo.com'), new \Exception('foo')); $ce = CommandTransferException::fromMultiTransferException($e); $this->assertEquals(2, count($ce)); }
public function testConvertsMultiExceptionIntoCommandTransfer() { $r1 = new Request('GET', 'http://foo.com'); $r2 = new Request('GET', 'http://foobaz.com'); $e = new MultiTransferException('Test', 123); $e->addSuccessfulRequest($r1)->addFailedRequest($r2); $ce = CommandTransferException::fromMultiTransferException($e); $this->assertInstanceOf('Guzzle\\Service\\Exception\\CommandTransferException', $ce); $this->assertEquals('Test', $ce->getMessage()); $this->assertEquals(123, $ce->getCode()); $this->assertSame(array($r1), $ce->getSuccessfulRequests()); $this->assertSame(array($r2), $ce->getFailedRequests()); }
/** * {@inheritdoc} */ public function execute($command) { if ($command instanceof CommandInterface) { $command = array($command); $singleCommand = true; } elseif (is_array($command)) { $singleCommand = false; } else { throw new InvalidArgumentException('Command must be a command or array of commands'); } $failureException = null; $requests = array(); $successful = new \SplObjectStorage(); foreach ($command as $c) { $c->setClient($this); // Set the state to new if the command was previously executed $request = $c->prepare()->setState(RequestInterface::STATE_NEW); $successful[$request] = $c; $requests[] = $request; $this->dispatch('command.before_send', array('command' => $c)); } try { $this->send($requests); } catch (MultiTransferException $failureException) { $failures = new \SplObjectStorage(); // Remove failed requests from the successful requests array and add to the failures array foreach ($failureException->getFailedRequests() as $request) { if (isset($successful[$request])) { $failures[$request] = $successful[$request]; unset($successful[$request]); } } } foreach ($successful as $success) { $this->dispatch('command.after_send', array('command' => $successful[$success])); } // Return the response or throw an exception if (!$failureException) { return $singleCommand ? end($command)->getResult() : $command; } elseif ($singleCommand) { // If only sending a single request, then don't use a CommandTransferException throw $failureException->getFirst(); } else { // Throw a CommandTransferException using the successful and failed commands $e = CommandTransferException::fromMultiTransferException($failureException); foreach ($failures as $failure) { $e->addFailedCommand($failures[$failure]); } foreach ($successful as $success) { $e->addSuccessfulCommand($successful[$success]); } throw $e; } }
/** * Execute multiple commands in parallel * * @param array|Traversable $commands Array of CommandInterface objects to execute * * @return array Returns an array of the executed commands * @throws Exception\CommandTransferException */ protected function executeMultiple($commands) { $requests = array(); $commandRequests = new \SplObjectStorage(); foreach ($commands as $command) { $request = $this->prepareCommand($command); $commandRequests[$request] = $command; $requests[] = $request; } try { $this->send($requests); foreach ($commands as $command) { $this->dispatch('command.after_send', array('command' => $command)); } return $commands; } catch (MultiTransferException $failureException) { // Throw a CommandTransferException using the successful and failed commands $e = CommandTransferException::fromMultiTransferException($failureException); // Remove failed requests from the successful requests array and add to the failures array foreach ($failureException->getFailedRequests() as $request) { if (isset($commandRequests[$request])) { $e->addFailedCommand($commandRequests[$request]); unset($commandRequests[$request]); } } // Always emit the command after_send events for successful commands foreach ($commandRequests as $success) { $e->addSuccessfulCommand($commandRequests[$success]); $this->dispatch('command.after_send', array('command' => $commandRequests[$success])); } throw $e; } }
protected function executeMultiple($commands) { $requests = array(); $commandRequests = new \SplObjectStorage(); foreach ($commands as $command) { $request = $this->prepareCommand($command); $commandRequests[$request] = $command; $requests[] = $request; } try { $this->send($requests); foreach ($commands as $command) { $this->dispatch('command.after_send', array('command' => $command)); } return $commands; } catch (MultiTransferException $failureException) { $e = CommandTransferException::fromMultiTransferException($failureException); foreach ($failureException->getFailedRequests() as $request) { if (isset($commandRequests[$request])) { $e->addFailedCommand($commandRequests[$request]); unset($commandRequests[$request]); } } foreach ($commandRequests as $success) { $e->addSuccessfulCommand($commandRequests[$success]); $this->dispatch('command.after_send', array('command' => $commandRequests[$success])); } throw $e; } }