コード例 #1
0
 /**
  * Converts a Guzzle exception into an Httplug exception.
  *
  * @param GuzzleExceptions\TransferException $exception
  * @param RequestInterface                   $request
  *
  * @return HttplugException
  */
 private function handleException(GuzzleExceptions\TransferException $exception, RequestInterface $request)
 {
     if ($exception instanceof GuzzleExceptions\ConnectException) {
         return new HttplugException\NetworkException($exception->getMessage(), $request, $exception);
     }
     if ($exception instanceof GuzzleExceptions\RequestException) {
         // Make sure we have a response for the HttpException
         if ($exception->hasResponse()) {
             $psr7Response = $this->createResponse($exception->getResponse());
             return new HttplugException\HttpException($exception->getMessage(), $request, $psr7Response, $exception);
         }
         return new HttplugException\RequestException($exception->getMessage(), $request, $exception);
     }
     return new HttplugException\TransferException($exception->getMessage(), 0, $exception);
 }
コード例 #2
0
ファイル: BaseController.php プロジェクト: EllynB/Incipio
 /**
  * {@inheritdoc}
  */
 public function handleGuzzleException(TransferException $exception)
 {
     $message = null;
     switch (true) {
         case $exception instanceof ConnectException:
             // Get exception message
             break;
         case $exception instanceof RequestException:
             $response = $exception->getResponse();
             switch ($response->getHeader('Content-Type')) {
                 case 'application/ld+json':
                     $body = $this->decode($response->getBody());
                     $type = $body['@type'];
                     switch ($type) {
                         case 'Error':
                             $message = sprintf('%s: %s', $body['hydra:title'], $body['hydra:description']);
                             break;
                         case 'ConstraintViolationList':
                             foreach ($body['violations'] as $violation) {
                                 $this->addFlash('error', sprintf('%s: %s', $violation['propertyPath'], $violation['message']));
                             }
                             return;
                     }
                     break;
                 case 'application/json':
                     $message = $this->decode($response->getBody());
                     break;
             }
             break;
     }
     // Other
     if (null === $message) {
         $message = $exception->getMessage();
         $message = true === empty($message) ? 'Une erreur est survenue.' : $message;
     }
     $this->addFlash('error', $message);
 }