/**
  * Sends an HTTP request.
  *
  * @param RequestInterface $request Request to send.
  * @param array            $options Request transfer options.
  *
  * @return PromiseInterface
  */
 public function __invoke(RequestInterface $request, array $options)
 {
     // Sleep if there is a delay specified.
     if (isset($options['delay'])) {
         usleep($options['delay'] * 1000);
     }
     $startTime = isset($options['on_stats']) ? microtime(true) : null;
     try {
         // Does not support the expect header.
         $request = $request->withoutHeader('Expect');
         // Append a content-length header if body size is zero to match
         // cURL's behavior.
         if (0 === $request->getBody()->getSize()) {
             $request = $request->withHeader('Content-Length', 0);
         }
         return $this->createResponse($request, $options, $this->createStream($request, $options), $startTime);
     } catch (\InvalidArgumentException $e) {
         throw $e;
     } catch (\Exception $e) {
         // Determine if the error was a networking error.
         $message = $e->getMessage();
         // This list can probably get more comprehensive.
         if (strpos($message, 'getaddrinfo') || strpos($message, 'Connection refused') || strpos($message, "couldn't connect to host")) {
             $e = new ConnectException($e->getMessage(), $request, $e);
         }
         $e = RequestException::wrapException($request, $e);
         $this->invokeStats($options, $request, $startTime, null, $e);
         return new RejectedPromise($e);
     }
 }
 public function testHasNoResponse()
 {
     $req = new Request('GET', '/');
     $prev = new \Exception();
     $e = new ConnectException('foo', $req, $prev, ['foo' => 'bar']);
     $this->assertSame($req, $e->getRequest());
     $this->assertNull($e->getResponse());
     $this->assertFalse($e->hasResponse());
     $this->assertEquals('foo', $e->getMessage());
     $this->assertEquals('bar', $e->getHandlerContext()['foo']);
     $this->assertSame($prev, $e->getPrevious());
 }
Ejemplo n.º 3
0
 /**
  * Sends an HTTP request.
  *
  * @param RequestInterface $request Request to send.
  * @param array            $options Request transfer options.
  *
  * @return PromiseInterface
  */
 public function __invoke(RequestInterface $request, array $options)
 {
     // Sleep if there is a delay specified.
     if (isset($options['delay'])) {
         usleep($options['delay'] * 1000);
     }
     try {
         // Does not support the expect header.
         $request = $request->withoutHeader('Expect');
         $stream = $this->createStream($request, $options);
         return $this->createResponse($request, $options, $stream);
     } catch (\InvalidArgumentException $e) {
         throw $e;
     } catch (\Exception $e) {
         // Determine if the error was a networking error.
         $message = $e->getMessage();
         // This list can probably get more comprehensive.
         if (strpos($message, 'getaddrinfo') || strpos($message, 'Connection refused') || strpos($message, "couldn't connect to host")) {
             $e = new ConnectException($e->getMessage(), $request, $e);
         }
         return new RejectedPromise(RequestException::wrapException($request, $e));
     }
 }
Ejemplo n.º 4
0
 /**
  * @param ConnectException $exception
  */
 private function raiseIfNoMoreServers($exception)
 {
     if (count($this->availableServers) == 0) {
         throw new ConnectException("No more servers available, exception from last server: " . $exception->getMessage(), $exception->getRequest());
     }
 }
 public static function create(\GuzzleHttp\Exception\ConnectException $e)
 {
     $message = sprintf('Connection error %s on %s', $e->getMessage(), $e->getRequest()->getUri());
     return new self($message, 0, $e);
 }
Ejemplo n.º 6
0
 /**
  * @param ConnectException $connectException
  * @return bool
  */
 public static function isCurlException(ConnectException $connectException)
 {
     return self::isCurlErrorString($connectException->getMessage());
 }