/**
  * Create a SPException from a TransferException
  *
  * @static
  * @access  public
  * @param   TransferException $e
  * @return  SPException
  */
 public static function fromTransferException(TransferException $e)
 {
     if ($e instanceof ParseException) {
         return new static('Could not parse response body as JSON', 0, $e);
     }
     if ($e instanceof RequestException) {
         $message = $e->getMessage();
         $code = $e->getCode();
         $matches = [];
         // if it's a cURL error, throw an exception with a more meaningful error
         if (preg_match('/^cURL error (?<code>\\d+): (?<message>.*)$/', $message, $matches)) {
             switch ($matches['code']) {
                 case 4:
                     // error triggered when libcURL doesn't support a protocol
                     $message = $matches['message'] . ' Hint: Check which SSL/TLS protocols your build of libcURL supports';
                     break;
                 case 35:
                     // this may happen when the SSLv2 or SSLv3 handshake fails
                     $message = $matches['message'] . ' Hint: Handshake failed. If supported, try using CURL_SSLVERSION_TLSv1_0';
                     break;
                 case 56:
                     // this can happen for several reasons
                     $message = $matches['message'] . ' Hint: Refer to the Troubleshooting.md document';
                     break;
                 default:
                     $message = $matches['message'];
                     break;
             }
             $code = $matches['code'];
         }
         return new static('Unable to make HTTP request: ' . $message, $code, $e);
     }
     return new static('Unable to make HTTP request', 0, $e);
 }
 /**
  * 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);
 }
 /**
  * @param \GuzzleHttp\Exception\TransferException $guzzleException
  *
  * @return string
  */
 public function getErrorMessage(TransferException $guzzleException)
 {
     return $guzzleException->getMessage();
 }
 /**
  * @param $e
  */
 private function handleTransferException(TransferException $e)
 {
     throw new SystemException($e->getMessage(), $e->getCode(), $e);
 }
Exemple #5
0
 /**
  * {@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);
 }