/**
  * @covers Guzzle\Http\Exception\RequestException
  */
 public function testRequestException()
 {
     $e = new RequestException('Message');
     $request = new Request('GET', 'http://www.guzzle-project.com/');
     $e->setRequest($request);
     $this->assertEquals($request, $e->getRequest());
 }
Example #2
0
 /**
  * @link https://github.com/guzzle/guzzle/issues/710
  */
 private function validateResponseWasSet(RequestInterface $request)
 {
     if ($request->getResponse()) {
         return true;
     }
     $body = $request instanceof EntityEnclosingRequestInterface ? $request->getBody() : null;
     if (!$body) {
         $rex = new RequestException('No response was received for a request with no body. This' . ' could mean that you are saturating your network.');
         $rex->setRequest($request);
         $this->removeErroredRequest($request, $rex);
     } elseif (!$body->isSeekable() || !$body->seek(0)) {
         // Nothing we can do with this. Sorry!
         $rex = new RequestException('The connection was unexpectedly closed. The request would' . ' have been retried, but attempting to rewind the' . ' request body failed.');
         $rex->setRequest($request);
         $this->removeErroredRequest($request, $rex);
     } else {
         $this->remove($request);
         // Add the request back to the batch to retry automatically.
         $this->requests[] = $request;
         $this->addHandle($request);
     }
     return false;
 }
 /**
  * Process a received response
  *
  * @param array $context Contextual information
  * @throws RequestException|BadResponseException on unsuccessful responses
  */
 protected function processResponse(array $context = array())
 {
     if (!$this->response) {
         // If no response, then processResponse shouldn't have been called
         $e = new RequestException('Error completing request');
         $e->setRequest($this);
         throw $e;
     }
     $this->state = self::STATE_COMPLETE;
     // A request was sent, but we don't know if we'll send more or if the final response will be successful
     $this->dispatch('request.sent', $this->getEventArray() + $context);
     // Some response processors will remove the response or reset the state (example: ExponentialBackoffPlugin)
     if ($this->state == RequestInterface::STATE_COMPLETE) {
         // The request completed, so the HTTP transaction is complete
         $this->dispatch('request.complete', $this->getEventArray());
         // If the response is bad, allow listeners to modify it or throw exceptions. You can change the response by
         // modifying the Event object in your listeners or calling setResponse() on the request
         if ($this->response->isError()) {
             $event = new Event($this->getEventArray());
             $this->getEventDispatcher()->dispatch('request.error', $event);
             // Allow events of request.error to quietly change the response
             if ($event['response'] !== $this->response) {
                 $this->response = $event['response'];
             }
         }
         // If a successful response was received, dispatch an event
         if ($this->response->isSuccessful()) {
             $this->dispatch('request.success', $this->getEventArray());
         }
     }
 }
Example #4
0
    public function curlExceptionProvider()
    {
        $requestException = new RequestException('request');
        $requestException->setRequest(new Request('GET', '/'));

        $curlException = new CurlException('curl');
        $curlException->setRequest(new Request('GET', '/'));
        return array(
            array($curlException, '\FOS\HttpCache\Exception\ProxyUnreachableException'),
            array($requestException, '\FOS\HttpCache\Exception\ProxyResponseException'),
            array(new \InvalidArgumentException('something'), '\InvalidArgumentException'),
        );
    }
Example #5
0
 /**
  * Transforms the exceptions thrown by \Guzzle\Http\Client
  * into Klout specific ones. If it doesn't know how to handle
  * the exception then it just re-throws it.
  *
  * @param  HttpRequestException        $e
  * @throws NotAuthorizedException
  * @throws ResourceNotFoundException
  * @throws ServiceUnavailableException
  * @throws Ambigous                    <HttpRequestException, \Guzzle\Http\Exception\ClientErrorResponseException, \Guzzle\Http\Exception\ServerErrorResponseException, \Guzzle\Http\Exception\MultiTransferException>
  * @return array:NULL
  */
 protected function handleHttpRequestException(HttpRequestException $e)
 {
     if ($e instanceof ClientErrorResponseException) {
         switch ($e->getResponse()->getStatusCode()) {
             case 403:
                 throw new NotAuthorizedException($e->getMessage());
                 break;
             case 404:
                 throw new ResourceNotFoundException($e->getMessage());
                 break;
         }
     } elseif ($e instanceof ServerErrorResponseException) {
         throw new ServiceUnavailableException($e->getMessage());
     } elseif ($e instanceof MultiTransferException) {
         if (count($e->getSuccessfulRequests()) == 0) {
             throw new ResourceNotFoundException($e->getMessage());
         }
         $responses = array();
         foreach ($e->getSuccessfulRequests() as $request) {
             $responses[] = $request->getResponse();
         }
         // In this case we return the valid responses
         return $responses;
     }
     // If we don't transform it to a Klout\Exception then
     // rethrow the original exception
     throw $e;
 }
Example #6
0
 protected function processResponse(array $context = array())
 {
     if (!$this->response) {
         $e = new RequestException('Error completing request');
         $e->setRequest($this);
         throw $e;
     }
     $this->state = self::STATE_COMPLETE;
     $this->dispatch('request.sent', $this->getEventArray() + $context);
     if ($this->state == RequestInterface::STATE_COMPLETE) {
         $this->dispatch('request.complete', $this->getEventArray());
         if ($this->response->isError()) {
             $event = new Event($this->getEventArray());
             $this->getEventDispatcher()->dispatch('request.error', $event);
             if ($event['response'] !== $this->response) {
                 $this->response = $event['response'];
             }
         }
         if ($this->response->isSuccessful()) {
             $this->dispatch('request.success', $this->getEventArray());
         }
     }
 }