/** * @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 addObjectUrl(ProcessEvent $e) { if ($e->getException()) { return; } $name = $e->getCommand()->getName(); if ($name === 'PutObject' || $name === 'CopyObject') { $e->getResult()['ObjectURL'] = $e->getRequest()->getUrl(); } elseif ($name === 'CompleteMultipartUpload') { $e->getResult()['ObjectURL'] = $e->getResult()['Location']; } }
public function onProcess(ProcessEvent $event) { // Only add a result object if no exception was encountered. if ($event->getException()) { return; } $command = $event->getCommand(); $operation = $this->description->getOperation($command->getName()); $rawConfig = $operation->toArray(); if (empty($rawConfig['responseParser'])) { return; } $parsedResponse = $this->parseResponse($event, $rawConfig['responseParser']); $event->setResult($parsedResponse); }
/** * @param ProcessEvent $event * @return ResponseInterface|null */ protected function createResult(ProcessEvent $event) { // Only add a result object if no exception was encountered. if ($event->getException()) { return null; } $command = $event->getCommand(); // Do not overwrite a previous result if ($event->getResult()) { return null; } $operation = $this->description->getOperation($command->getName()); $responseClass = $operation->getResponseModel(); if ($responseClass === null) { throw new Exception\RuntimeException(sprintf('No response class configured for operation "%s"', $command->getName())); } if (!class_exists($responseClass)) { throw new Exception\RuntimeException(sprintf('Response class "%s" of operation "%s" does not exist', $responseClass, $command->getName())); } /** @todo We could check if the response class implements ResponseInterface */ /** @var ResponseInterface $responseClass */ return $responseClass::fromOperation($operation, $event); }
public function onProcess(ProcessEvent $event) { // Only add a result object if no exception was encountered. if ($event->getException()) { return; } $command = $event->getCommand(); // Do not overwrite a previous result if ($event->getResult()) { return; } $operation = $this->description->getOperation($command->getName()); // Add a default Model as the result if no matching schema was found. if (!($modelName = $operation->getResponseModel())) { $event->setResult([]); return; } $model = $operation->getServiceDescription()->getModel($modelName); if (!$model) { throw new \RuntimeException("Unknown model: {$modelName}"); } $event->setResult($this->visit($model, $event)); }
/** * @param ProcessEvent $event Process event of the attempt. * @param array $acceptor Acceptor configuration being checked. * * @return bool */ private function matchesError(ProcessEvent $event, array $acceptor) { /** @var \Aws\Exception\AwsException $exception */ $exception = $event->getException(); if (!$exception) { return false; } $actual = $exception->getAwsErrorCode(); if ($actual == $acceptor['expected']) { $event->setResult(true); // a.k.a do not throw the exception. return true; } return false; }