protected function createApiMock($response, $statusCode = 200) { $jsonFile = $this->createJsonFile(); $jobsMethods = array('collectCloverXml', 'getJsonFile', 'collectGitInfo', 'collectEnvVars', 'dumpJsonFile', 'send'); $api = $this->getMockBuilder('Satooshi\\Bundle\\CoverallsV1Bundle\\Api\\Jobs')->disableOriginalConstructor()->setMethods($jobsMethods)->getMock(); $api->expects($this->once())->method('collectCloverXml')->with()->will($this->returnSelf()); $api->expects($this->once())->method('getJsonFile')->with()->will($this->returnValue($jsonFile)); $api->expects($this->once())->method('collectGitInfo')->with()->will($this->returnSelf()); $api->expects($this->once())->method('collectEnvVars')->with($this->equalTo($_SERVER))->will($this->returnSelf()); $api->expects($this->once())->method('dumpJsonFile')->with()->will($this->returnSelf()); $request = $this->getMockBuilder('\\GuzzleHttp\\Psr7\\Request')->setConstructorArgs(['POST', '/'])->getMock(); if ($statusCode === 200) { $api->expects($this->once())->method('send')->with()->will($this->returnValue($response)); } else { if ($statusCode === null) { $exception = \GuzzleHttp\Exception\ConnectException::create($request); } elseif ($statusCode === 422) { $exception = \GuzzleHttp\Exception\ClientException::create($request, $response); } else { $exception = \GuzzleHttp\Exception\ServerException::create($request, $response); } $api->expects($this->once())->method('send')->with()->will($this->throwException($exception)); } return $api; }
/** * 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()); }
/** * 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)); } }
/** * @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); }
/** * Handle various Guzzle exceptions * * @param ConnectException|ClientException $exception Guzzle exception instance * * @return mixed[] $messages Array of errorCodes and messages */ protected function handleExceptions($exception) { $messages = array(); if ($exception instanceof ConnectException || $exception instanceof ClientException) { $messages = ['errorCode' => $exception->getCode(), 'message' => json_decode($exception->getResponseBodySummary($exception->getResponse()), true)['error']]; } else { } return $messages; }
/** * @param ConnectException $connectException * @return bool */ public static function isCurlException(ConnectException $connectException) { return self::isCurlErrorString($connectException->getMessage()); }