/** * @internal * @param ProcessEvent $event */ public function handleError(ProcessEvent $event) { if (!($exception = $event->getException())) { return; } $name = $event->getCommand()->getName(); $operation = $event->getClient()->getDescription()->getOperation($name); $errors = $operation->getErrorResponses(); $response = $event->getResponse(); // We iterate through each errors in service description. If the descriptor contains both a phrase and // status code, there must be an exact match of both. Otherwise, a match of status code is enough $bestException = null; foreach ($errors as $error) { $code = (int) $error['code']; if ($response->getStatusCode() !== $code) { continue; } if (isset($error['phrase']) && !($error['phrase'] === $response->getReasonPhrase())) { continue; } $bestException = $error['class']; // If there is an exact match of phrase + code, then we cannot find a more specialized exception in // the array, so we can break early instead of iterating the remaining ones if (isset($error['phrase'])) { break; } } if (null !== $bestException) { throw new $bestException($response->getReasonPhrase(), $event->getTransaction(), $exception); } // If we reach here, no exception could be match from descriptor, and Guzzle exception will propagate }
public function testHasData() { $command = $this->getMock('GuzzleHttp\\Command\\CommandInterface'); $client = $this->getMock('GuzzleHttp\\Command\\ServiceClientInterface'); $trans = new CommandTransaction($client, $command); $request = new Request('GET', 'http://httbin.org'); $response = new Response(200); $trans->setRequest($request); $trans->setResponse($response); $event = new ProcessEvent($trans); $this->assertSame($command, $event->getCommand()); $this->assertSame($client, $event->getClient()); $this->assertSame($request, $event->getRequest()); $this->assertSame($response, $event->getResponse()); $this->assertSame($trans, $event->getTransaction()); $this->assertNull($event->getResult()); }
protected function visit(Parameter $model, ProcessEvent $event) { $result = []; $context = ['client' => $event->getClient(), 'visitors' => []]; $command = $event->getCommand(); $response = $event->getResponse(); if ($model->getType() == 'object') { $this->visitOuterObject($model, $result, $command, $response, $context); } elseif ($model->getType() == 'array') { $this->visitOuterArray($model, $result, $command, $response, $context); } else { throw new \InvalidArgumentException('Invalid response model: ' . $model->getType()); } // Call the after() method of each found visitor foreach ($context['visitors'] as $visitor) { $visitor->after($command, $response, $model, $result, $context); } return $result; }