public function checkForPermanentRedirect(ProcessEvent $e) { $res = $e->getResponse(); if ($res && $res->getStatusCode() == 301) { throw new PermanentRedirectException('Encountered a permanent redirect while requesting ' . $e->getRequest()->getUrl(), $e->getTransaction()); } }
/** * @param ProcessEvent $event */ public function onProcess(ProcessEvent $event) { $jsonResponse = $event->getResponse()->json(); if (is_array($jsonResponse) && array_key_exists('error', $jsonResponse) && array_key_exists('errorcode', $jsonResponse)) { throw new BadResponseException($jsonResponse['error'], $event->getRequest()); } }
/** * @param Operation $operation * @param ProcessEvent $event * @return ResponseInterface */ public static function fromOperation(Operation $operation, ProcessEvent $event) { $operationConfig = $operation->toArray(); if (!isset($operationConfig['responseDataRoot'])) { throw new Exception\RuntimeException(sprintf('No response data root configured for operation "%s"', $operation->getName())); } return new static($event->getResponse(), $operationConfig['responseDataRoot']); }
public function testCanCreateWithResult() { $command = $this->getMock('GuzzleHttp\\Command\\CommandInterface'); $client = $this->getMock('GuzzleHttp\\Command\\ServiceClientInterface'); $trans = new CommandTransaction($client, $command); $trans->setResult('foo'); $event = new ProcessEvent($trans); $this->assertSame('foo', $event->getResult()); }
private function parseResponse(ProcessEvent $event, $responseParserClass) { if (class_exists($responseParserClass) == false) { throw new RuntimeException("Unknown response parser: {$responseParserClass}"); } $result = $event->getResult(); if (is_array($result) && in_array(ArrayParsable::class, class_implements($responseParserClass))) { /* @var ArrayParsable $responseParserClass */ return $responseParserClass::fromArray($result); } throw new RuntimeException("Response parser {$responseParserClass} does not implement " . ArrayParsable::class); }
public function onProcess(ProcessEvent $event) { if ($event->getCommand()->getName() !== 'ReceiveMessage') { return; } $result = $event->getResult(); if (isset($result['Messages'])) { foreach ($result['Messages'] as $message) { if ($message['MD5OfBody'] != md5($message['Body'])) { throw new SqsException('Body MD5 mismatch for ' . var_export($message, true), $event->getTransaction()); } } } }
/** * @internal * * @param ProcessEvent $event */ public function onProcess(ProcessEvent $event) { // Decode the JSON response $data = ''; try { $data = $event->getResponse()->getBody()->getContents(); $json = $event->getResponse()->json(); } catch (\RuntimeException $e) { // Invalid response; make our own error message $json = ['ok' => $data === 'ok', 'error' => $data]; } if (!isset($json['ok']) || $json['ok'] === false) { throw new SlackApiException($json['error'], $event->getTransaction()); } }
/** * Callback for process event. */ public function onProcess(ProcessEvent $event) { $response = $event->getResponse(); // var_dump((string) $event->getRequest()); // var_dump((string) $response); if (!$response) { return; } try { $json = $response->json(); } catch (\RuntimeException $e) { return; } if (NULL === $response || $json['success']) { return; } $command = $event->getCommand(); $message = ''; $this->operation = $this->description->getOperation($command->getName()); // string, // simple array, // assoc array. $errors = $json['errors']; $category = $this->operation->getParam('modelName')->getDefault(); $method = $this->operation->getName(); $suffix = " on {$category} operation { {$method} }"; if (is_string($errors)) { $message = 'Common error: "' . $errors . '"' . $suffix; } elseif (is_array($errors)) { $keys = array_keys($errors); $message = $errors[$keys[0]]; $property = $keys[0]; $alias = $this::getParamBySentAs($keys[0]); if ($alias) { $message = str_replace($property, $alias, $message); } $message = 'Error: "' . $message . '"' . $suffix; } $e = new NpErrorException($message, $event->getTransaction()); $e->setCategory($category); $e->setVerboseMessage($message); throw $e; }
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; }
/** * Stops propagation of ProcessEvents when using Batching * * @param ProcessEvent $event The Process Event * * @return bool */ public function onProcess(ProcessEvent $event) { $command = $event->getCommand(); $operation = $this->description->getOperation($command->getName()); if (!$operation->getData('batching')) { return false; } $event->stopPropagation(); return true; }
public function onProcess(ProcessEvent $event) { $response = $event->getResponse(); if (!$response) { return; } try { $xml = $response->xml(); } catch (\RuntimeException $e) { return; } $eanError = $xml->EanError ?: $xml->EanWsError; if ($eanError) { $e = new EanErrorException((string) $eanError->presentationMessage, $event->getTransaction()); $e->setHandling((string) $eanError->handling); $e->setCategory((string) $eanError->category); $e->setVerboseMessage((string) $eanError->verboseMessage); $e->setItineraryId((string) $eanError->itineraryId); throw $e; } }
/** * @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']; } }
/** * @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); }
/** * @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; }
/** * @param Operation $operation * @param ProcessEvent $event * @return ResponseInterface */ public static function fromOperation(Operation $operation, ProcessEvent $event) { return new static($event->getResponse()); }
/** * Callback for process event. */ public function onProcess(ProcessEvent $event) { $result = $event->getResult(); !isset($result['data']) ?: $event->setResult($result['data']); }