From fe8ab12c4bc20a63330d11e3680549178deb57ba Mon Sep 17 00:00:00 2001 From: Tanbin Siyam Date: Sun, 19 Dec 2021 02:44:03 +0600 Subject: [PATCH 1/2] added data and errors from response in QueryError exception --- src/Exception/QueryError.php | 31 +++++++++++- tests/QueryErrorTest.php | 95 +++++++++++++++++++++++++++++++++++- 2 files changed, 124 insertions(+), 2 deletions(-) diff --git a/src/Exception/QueryError.php b/src/Exception/QueryError.php index 0a2e6ee..4288d26 100644 --- a/src/Exception/QueryError.php +++ b/src/Exception/QueryError.php @@ -17,6 +17,14 @@ class QueryError extends RuntimeException * @var array */ protected $errorDetails; + /** + * @var array + */ + protected $data; + /** + * @var array + */ + protected $errors; /** * QueryError constructor. @@ -26,6 +34,11 @@ class QueryError extends RuntimeException public function __construct($errorDetails) { $this->errorDetails = $errorDetails['errors'][0]; + $this->data = []; + if (!empty($errorDetails['data'])) { + $this->data = $errorDetails['data']; + } + $this->errors = $errorDetails['errors']; parent::__construct($this->errorDetails['message']); } @@ -36,4 +49,20 @@ public function getErrorDetails() { return $this->errorDetails; } -} \ No newline at end of file + + /** + * @return array + */ + public function getData() + { + return $this->data; + } + + /** + * @return array + */ + public function getErrors() + { + return $this->errors; + } +} diff --git a/tests/QueryErrorTest.php b/tests/QueryErrorTest.php index 4ee121c..1d2336b 100644 --- a/tests/QueryErrorTest.php +++ b/tests/QueryErrorTest.php @@ -48,4 +48,97 @@ public function testConstructQueryError() $queryError->getErrorDetails() ); } -} \ No newline at end of file + + /** + * @covers \GraphQL\Exception\QueryError::__construct + * @covers \GraphQL\Exception\QueryError::getErrorDetails + */ + public function testConstructQueryErrorWhenResponseHasData() + { + $errorData = [ + 'errors' => [ + [ + 'message' => 'first error message', + 'location' => [ + [ + 'line' => 1, + 'column' => 3, + ] + ], + ], + [ + 'message' => 'second error message', + 'location' => [ + [ + 'line' => 2, + 'column' => 4, + ] + ], + ], + ], + 'data' => [ + 'someField' => [ + [ + 'data' => 'value', + ], + [ + 'data' => 'value', + ] + ] + ] + ]; + + $queryError = new QueryError($errorData); + $this->assertEquals('first error message', $queryError->getMessage()); + $this->assertEquals( + [ + 'message' => 'first error message', + 'location' => [ + [ + 'line' => 1, + 'column' => 3, + ] + ] + ], + $queryError->getErrorDetails() + ); + + $this->assertEquals( + [ + [ + 'message' => 'first error message', + 'location' => [ + [ + 'line' => 1, + 'column' => 3, + ] + ] + ], + [ + 'message' => 'second error message', + 'location' => [ + [ + 'line' => 2, + 'column' => 4, + ] + ] + ] + ], + $queryError->getErrors() + ); + + $this->assertEquals( + [ + 'someField' => [ + [ + 'data' => 'value', + ], + [ + 'data' => 'value', + ] + ] + ], + $queryError->getData() + ); + } +} From cf4a2d9ab17740fbc626fbad63f55df5a80b1394 Mon Sep 17 00:00:00 2001 From: Tanbin Siyam Date: Sun, 19 Dec 2021 02:51:53 +0600 Subject: [PATCH 2/2] test covered for the case when there is no data with error response --- tests/QueryErrorTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/QueryErrorTest.php b/tests/QueryErrorTest.php index 1d2336b..414e58b 100644 --- a/tests/QueryErrorTest.php +++ b/tests/QueryErrorTest.php @@ -47,6 +47,7 @@ public function testConstructQueryError() ], $queryError->getErrorDetails() ); + $this->assertEquals([], $queryError->getData()); } /**