From 2a25cf4b6535c3be8403d6e4ad9d755828cfcae8 Mon Sep 17 00:00:00 2001 From: James Sanders Date: Wed, 9 May 2018 08:57:40 +0100 Subject: [PATCH] Pass the transfer exception through as "previous" (#13) - Provides access to server response to help identify errors --- src/Client.php | 2 +- tests/ClientTest.php | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Client.php b/src/Client.php index 0f7ef1f..bcaacac 100644 --- a/src/Client.php +++ b/src/Client.php @@ -32,7 +32,7 @@ public function query(string $query, array $variables = []): Response try { $response = $this->httpClient->request('POST', '', $options); } catch (TransferException $e) { - throw new \RuntimeException('Network Error.'); + throw new \RuntimeException('Network Error.'.$e->getMessage(), 0, $e); } return $this->responseBuilder->build($response); diff --git a/tests/ClientTest.php b/tests/ClientTest.php index bb8d12a..bc28586 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -31,6 +31,28 @@ public function testSimpleQueryWhenHasNetworkErrors() $this->client->query($query); } + public function testCanRetrievePreviousExceptionWhenSimpleQueryHasErrors() + { + $previousException = null; + try { + $originalException = new \GuzzleHttp\Exception\ServerException( + 'Server side error', + $this->createMock(\Psr\Http\Message\RequestInterface::class) + ); + + $this->httpClient->expects($this->once()) + ->method('request') + ->willThrowException($originalException); + + $query = $this->getSimpleQuery(); + $this->client->query($query); + } catch (\Exception $e) { + $previousException = $e->getPrevious(); + } finally { + $this->assertSame($originalException, $previousException); + } + } + public function testSimpleQueryWhenInvalidJsonIsReceived() { $query = $this->getSimpleQuery();