Skip to content

added data and errors in QueryError exception #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/Exception/QueryError.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ class QueryError extends RuntimeException
* @var array
*/
protected $errorDetails;
/**
* @var array
*/
protected $data;
/**
* @var array
*/
protected $errors;

/**
* QueryError constructor.
Expand All @@ -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']);
}

Expand All @@ -36,4 +49,20 @@ public function getErrorDetails()
{
return $this->errorDetails;
}
}

/**
* @return array
*/
public function getData()
{
return $this->data;
}

/**
* @return array
*/
public function getErrors()
{
return $this->errors;
}
}
96 changes: 95 additions & 1 deletion tests/QueryErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,99 @@ public function testConstructQueryError()
],
$queryError->getErrorDetails()
);
$this->assertEquals([], $queryError->getData());
}
}

/**
* @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()
);
}
}