/**
  * @param   RequestException $ex
  * @return  AccessDeniedException|EntityNotFoundException|EntityValidationException|RequiredFieldMissingException|UnauthorizedClientException|CaravanaHttpException
  */
 public static function parseRequestException(RequestException $ex)
 {
     $code = $ex->getCode();
     $body = json_decode($ex->getResponse()->getBody()->getContents(), true);
     if (CaravanaExceptionFactory::isEntityValidationException($code, $body)) {
         return new EntityValidationException(AU::get($body['Entity']), AU::get($body['field']), AU::get($body['reason']), AU::get($body['providedValue']), $ex->getPrevious());
     } else {
         if (CaravanaExceptionFactory::isRequiredFieldMissingException($code, $body)) {
             return new RequiredFieldMissingException(AU::get($body['Entity']), AU::get($body['field']), $ex->getPrevious());
         } else {
             if (CaravanaExceptionFactory::isUnauthorizedClientException($code, $body)) {
                 return new UnauthorizedClientException($ex->getMessage(), $ex->getPrevious());
             } else {
                 if (CaravanaExceptionFactory::isAccessDeniedException($code, $body)) {
                     return new AccessDeniedException($ex->getMessage(), $ex->getPrevious());
                 } else {
                     if (CaravanaExceptionFactory::isEntityNotFoundException($code, $body)) {
                         return new EntityNotFoundException(AU::get($body['Entity']), AU::get($body['field']), AU::get($body['providedValue']), $ex->getPrevious());
                     }
                 }
             }
         }
     }
     //  Give up and default it to CaravanaException
     return new CaravanaHttpException($ex->getMessage(), $ex->getCode(), null, $ex->getPrevious());
 }
Пример #2
0
 /**
  * Converts a Guzzle exception into an Httplug exception.
  *
  * @param RequestException $exception
  *
  * @return Exception
  */
 private function createException(RequestException $exception)
 {
     if ($exception->hasResponse()) {
         return new HttpException($exception->getMessage(), $exception->getRequest(), $exception->getResponse(), $exception);
     }
     return new NetworkException($exception->getMessage(), $exception->getRequest(), $exception);
 }
Пример #3
0
 private function handleException(RequestException $exception)
 {
     if ($exception instanceof BadResponseException) {
         return $exception->getResponse();
     }
     throw new Exception($exception->getMessage());
 }
Пример #4
0
    protected function failBecauseOfHttpError(RequestException $e)
    {
        $this->fail(sprintf(<<<'EOL'
failed to create user: HTTP ERROR
%s
EOL
, $e->getMessage()));
    }
Пример #5
0
 /**
  * throwRequestException
  *
  * @param \GuzzleHttp\Exception\RequestException $e
  * @param string $className
  * @throws Exceptions\RequestException
  */
 protected function throwRequestException($e, $className)
 {
     $response = $e->getResponse();
     $rawBody = (string) $response->getBody();
     $body = json_decode((string) $rawBody);
     $body = $body === null ? $rawBody : $body;
     $exception = new $className($e->getMessage(), $e->getCode());
     $exception->setBody($body);
     throw $exception;
 }
 public function testHasRequestAndResponse()
 {
     $req = new Request('GET', '/');
     $res = new Response(200);
     $e = new RequestException('foo', $req, $res);
     $this->assertSame($req, $e->getRequest());
     $this->assertSame($res, $e->getResponse());
     $this->assertTrue($e->hasResponse());
     $this->assertEquals('foo', $e->getMessage());
 }
Пример #7
0
 /**
  * Wraps an API exception in the appropriate domain exception.
  *
  * @param RequestException $e The API exception
  *
  * @return HttpException
  */
 public static function wrap(RequestException $e)
 {
     $response = $e->getResponse();
     if ($errors = self::errors($response)) {
         $class = self::exceptionClass($response, $errors[0]);
         $message = implode(', ', array_map('strval', $errors));
     } else {
         $class = self::exceptionClass($response);
         $message = $e->getMessage();
     }
     return new $class($message, $errors, $e->getRequest(), $response, $e);
 }
 public function __construct(RequestException $e)
 {
     $message = $e->getMessage();
     if ($e instanceof ClientException) {
         $response = $e->getResponse();
         $responseBody = $response->getBody()->getContents();
         $this->errorDetails = $responseBody;
         $message .= ' [details] ' . $this->errorDetails;
     } elseif ($e instanceof ServerException) {
         $message .= ' [details] Zendesk may be experiencing internal issues or undergoing scheduled maintenance.';
     } elseif (!$e->hasResponse()) {
         $request = $e->getRequest();
         // Unsuccessful response, log what we can
         $message .= ' [url] ' . $request->getUri();
         $message .= ' [http method] ' . $request->getMethod();
     }
     parent::__construct($message, $e->getCode());
 }
 public function __construct(RequestException $e)
 {
     $this->request = $e->getRequest();
     $this->response = $e->getResponse();
     $simpleMessage = $e->getMessage();
     $code = 0;
     if ($this->response) {
         try {
             $decodedJson = Utils::jsonDecode((string) $this->response->getBody(), true);
             if ($decodedJson && isset($decodedJson['errorType'])) {
                 $simpleMessage = $decodedJson['errorType'] . ' ' . $decodedJson['message'];
             }
         } catch (\InvalidArgumentException $e) {
             // Not Json
         }
         $code = $this->response->getStatusCode();
     }
     $responseDescription = $this->response ? (string) $this->response : 'none';
     $requestDescription = $this->request ? (string) $this->request : 'none';
     $message = sprintf("%s\n\nRequest: %s\n\nResponse: %s\n\n", $simpleMessage, $requestDescription, $responseDescription);
     parent::__construct($message, $code, $e);
 }
 /**
  * Handles a Request Exception.
  *
  * @param RequestException $e The request exception.
  *
  * @return void
  */
 protected function onRequestException(RequestException $e)
 {
     $request = $e->getRequest();
     $response = $e->getResponse();
     if (!$response) {
         throw new RuntimeException($e->getMessage(), $e->getCode());
     }
     $statusCode = $response->getStatusCode();
     $isClientError = $response->isClientError();
     $isServerError = $response->isServerError();
     if ($isClientError || $isServerError) {
         $content = $response->getContent();
         $error = $response->getError();
         $description = $response->getErrorDescription();
         if (400 === $statusCode) {
             throw new BadRequestException($description, $error, $statusCode, $response, $request);
         }
         if (401 === $statusCode) {
             $otp = (string) $response->getHeader('X-Bitreserve-OTP');
             if ('required' === $otp) {
                 $description = 'Two factor authentication is enabled on this account';
                 throw new TwoFactorAuthenticationRequiredException($description, $error, $statusCode, $response, $request);
             }
             throw new AuthenticationRequiredException($description, $error, $statusCode, $response, $request);
         }
         if (404 === $statusCode) {
             $description = sprintf('Object or route not found: %s', $request->getPath());
             throw new NotFoundException($description, 'not_found', $statusCode, $response, $request);
         }
         if (429 === $statusCode) {
             $rateLimit = $response->getApiRateLimit();
             $description = sprintf('You have reached Uphold API limit. API limit is: %s. Your remaining requests will be reset at %s.', $rateLimit['limit'], date('Y-m-d H:i:s', $rateLimit['reset']));
             throw new ApiLimitExceedException($description, $error, $statusCode, $response, $request);
         }
         throw new RuntimeException($description, $error, $statusCode, $response, $request);
     }
 }
 /**
  * @param RequestException $exception
  * @throws RequestFailedException
  */
 public static function handleGuzzleRequestException(RequestException $exception)
 {
     $message = sprintf('Request failed due: "%s".', $exception->getMessage());
     throw new RequestFailedException($message, $exception->getCode(), $exception);
 }
 private function facebookApiException($message, RequestInterface $request, ResponseInterface $response = null, RequestException $exception = null)
 {
     if (null !== $exception) {
         $message .= $exception->getMessage();
     }
     if (null !== $this->logger) {
         $context = array('request' => $request);
         if (null !== $response) {
             $context['response'] = (string) $response;
         }
         $this->logger->error($message, $context);
     }
     return new FacebookApiException($message);
 }
Пример #13
0
 protected function createException(GuzzleRequestException $e)
 {
     $statusCode = $e->getResponse()->getStatusCode();
     switch ($statusCode) {
         case 401:
             $authInfo = $this->parseAuthHeader($e->getResponse()->getHeader('www-authenticate'));
             $code = array_key_exists('error', $authInfo) ? $authInfo['error'] : 401;
             $message = array_key_exists('error_description', $authInfo) ? $authInfo['error_description'] : $e->getMessage();
             return new UnauthorizedException($message, $code, $e);
         default:
             return new ApiRequestException('Api Request failed: ' . $e->getMessage(), 0, $e);
     }
 }
Пример #14
0
 /**
  * @param RequestException $e
  * @return ApiException
  */
 private function apiException(RequestException $e)
 {
     $message = $e->hasResponse() ? $e->getResponse()->getBody()->getContents() : $e->getMessage();
     $code = $e->hasResponse() ? $e->getResponse()->getStatusCode() : null;
     return new ApiException($message, $code);
 }
 /**
  * Manage exception in http call.
  *
  * @param RequestException $exception
  *
  * @throw RuntimeException.
  */
 protected function manageException(RequestException $exception)
 {
     $errorMessage = $exception->getMessage() . ' Request: ' . $exception->getRequest();
     if ($exception->hasResponse()) {
         $errorMessage .= ' ErrorCode: ' . $exception->getResponse()->getStatusCode() . ' Response: ' . $exception->getResponse()->getBody();
     }
     throw new RuntimeException($errorMessage);
 }
Пример #16
0
 /**
  * @author WN
  * @param Exception\RequestException $e
  * @return array
  */
 private function formatRequestException(Exception\RequestException $e)
 {
     return ['message' => $e->getMessage(), 'request' => ['headers' => $e->getRequest()->getHeaders(), 'body' => $e->getRequest()->getBody()->getContents(), 'method' => $e->getRequest()->getMethod(), 'uri' => $e->getRequest()->getUri()]];
 }
Пример #17
0
 /**
  * Prepares an array of important exception parts based on composition of a
  * given exception.
  *
  * @param  RequestException  $requestException
  *
  * @return array
  */
 private function getRequestExceptionParts(RequestException $requestException)
 {
     $response = $requestException->getResponse();
     $parts = [];
     $parts['reason'] = $response ? $response->getReasonPhrase() : $requestException->getMessage();
     $parts['code'] = $response ? $response->getStatusCode() : $requestException->getCode();
     $parts['body'] = $response ? $response->getBody() : null;
     return $parts;
 }
 private function handleException(RequestException $exception)
 {
     if ($exception->getCode() === self::STREAM_DOES_NOT_EXIST) {
         throw new StreamDoesNotExist($exception->getMessage());
     }
     if ($exception->getCode() === self::REQUEST_BODY_INVALID && empty(json_decode($exception->getRequest()->getBody()))) {
         throw new CannotWriteStreamWithoutEvents($exception->getMessage());
     }
     throw new EventStoreConnectionFailed($exception->getMessage());
 }
 /**
  * @param \GuzzleHttp\Exception\RequestException $previous
  *
  * @return \Riak\Client\Core\Transport\RiakTransportException
  */
 public static function httpRequestException(RequestException $previous)
 {
     return new self($previous->getMessage(), $previous->getCode(), $previous);
 }
Пример #20
0
 public function createCommandException(CommandTransaction $transaction, RequestException $previous)
 {
     $cn = 'GuzzleHttp\\Command\\Exception\\CommandException';
     if ($response = $transaction->getResponse()) {
         $statusCode = (string) $response->getStatusCode();
         if ($statusCode[0] == '4') {
             $cn = 'GuzzleHttp\\Command\\Exception\\CommandClientException';
         } elseif ($statusCode[0] == '5') {
             $cn = 'GuzzleHttp\\Command\\Exception\\CommandServerException';
         }
     }
     return new $cn("Error executing command: " . $previous->getMessage(), $transaction, $previous);
 }
Пример #21
0
 public function __construct(\GuzzleHttp\Exception\RequestException $exception)
 {
     parent::__construct($exception->getMessage(), $exception->getRequest(), $exception->getResponse(), $exception->getPrevious(), $exception->getHandlerContext());
 }
Пример #22
0
 /**
  * Emits an error event for a request and accounts for the propagation
  * of an error event being stopped to prevent the exception from being
  * thrown.
  *
  * @param TransactionInterface $transaction
  * @param \Exception           $e
  * @param array                $stats
  *
  * @throws \GuzzleHttp\Exception\RequestException
  */
 public static function emitError(TransactionInterface $transaction, \Exception $e, array $stats = [])
 {
     $request = $transaction->getRequest();
     // Convert non-request exception to a wrapped exception
     if (!$e instanceof RequestException) {
         $e = new RequestException($e->getMessage(), $request, null, $e);
     }
     // Mark the exception as having been emitted for an error event. This
     // works in tandem with the emitBefore method to prevent the error
     // event from being triggered twice for the same exception.
     $e->emittedError(true);
     // Dispatch an event and allow interception
     if (!$request->getEmitter()->emit('error', new ErrorEvent($transaction, $e, $stats))->isPropagationStopped()) {
         throw $e;
     }
 }
Пример #23
0
 private function standardizeExceptionResponse(RequestException $e)
 {
     $response = new stdClass();
     $response->status = $e->getCode();
     $response->description = $e->getMessage();
     return $response;
 }