Error formatting: display error extensions under extensions key

This commit is contained in:
Vladimir Razuvaev 2018-08-07 23:20:39 +07:00
parent 39df711eac
commit f4008f0fb2
6 changed files with 106 additions and 10 deletions

View File

@ -1,4 +1,24 @@
# Changelog
## dev-master
- Spec compliance: error extensions are displayed under `extensions` key
#### v0.12.5
- Execution performance optimization for lists
#### v0.12.4
- Allow stringeable objects to be serialized by StringType (#303)
#### v0.12.3
- StandardServer: add support for the multipart/form-data content type (#300)
#### v0.12.2
- SchemaPrinter: Use multi-line block for trailing quote (#294)
#### v0.12.1
- Fixed bug in validation rule OverlappingFieldsCanBeMerged (#292)
- Added one more breaking change note in UPGRADE.md (#291)
- Spec compliance: remove `data` entry from response on top-level error (#281)
## v0.12.0
- RFC: Block String (multi-line strings via triple-quote """string""")
- GraphQL Schema SDL: Descriptions as strings (including multi-line)

View File

@ -1,3 +1,40 @@
## Upgrade v0.12.x > dev-master
### Breaking: minimum supported version of PHP
New minimum required version of PHP is **7.1+**
### Breaking: errors formatting changed according to spec
Extensions assigned to errors are shown under `extensions` key
```php
$e = new Error(
'msg',
null,
null,
null,
null,
null,
['foo' => 'bar']
);
```
Formatting before the change:
```
'errors' => [
[
'message' => 'msg',
'foo' => 'bar'
]
]
```
After the change:
```
'errors' => [
[
'message' => 'msg',
'extensions' => ['foo' => 'bar'],
]
]
```
## Upgrade v0.11.x > v0.12.x
### Breaking: Minimum supported version is PHP5.6

View File

@ -326,10 +326,6 @@ class Error extends \Exception implements \JsonSerializable, ClientAware
'message' => $this->getMessage(),
];
if ($this->getExtensions()) {
$arr = array_merge($this->getExtensions(), $arr);
}
$locations = Utils::map(
$this->getLocations(),
function (SourceLocation $loc) {
@ -343,6 +339,9 @@ class Error extends \Exception implements \JsonSerializable, ClientAware
if (! empty($this->path)) {
$arr['path'] = $this->path;
}
if (! empty($this->extensions)) {
$arr['extensions'] = $this->extensions;
}
return $arr;
}

View File

@ -187,23 +187,21 @@ class FormattedError
}
if ($e instanceof Error) {
if ($e->getExtensions()) {
$formattedError = array_merge($e->getExtensions(), $formattedError);
}
$locations = Utils::map(
$e->getLocations(),
function (SourceLocation $loc) {
return $loc->toSerializableArray();
}
);
if (! empty($locations)) {
$formattedError['locations'] = $locations;
}
if (! empty($e->path)) {
$formattedError['path'] = $e->path;
}
if (! empty($e->getExtensions())) {
$formattedError['extensions'] = $e->getExtensions();
}
}
if ($debug) {

View File

@ -146,6 +146,9 @@ class ErrorTest extends TestCase
);
$this->assertEquals(['foo' => 'bar'], $e->getExtensions());
$this->assertEquals(['message' => 'msg', 'foo' => 'bar'], $e->toSerializableArray());
$this->assertEquals([
'message' => 'msg',
'extensions' => ['foo' => 'bar'],
], $e->toSerializableArray());
}
}

View File

@ -5,12 +5,51 @@ use GraphQL\Error\Error;
use GraphQL\Error\FormattedError;
use GraphQL\Language\Parser;
use GraphQL\Language\Source;
use GraphQL\Language\SourceLocation;
use PHPUnit\Framework\TestCase;
class PrintErrorTest extends TestCase
{
// Describe printError
/**
* @it prints an line numbers with correct padding
*/
public function testPrintsAnLineNumbersWithCorrectPadding()
{
$singleDigit = new Error(
'Single digit line number with no padding',
null,
new Source('*', 'Test', new SourceLocation(9, 1)),
[0]
);
$actual = FormattedError::printError($singleDigit);
$expected = 'Single digit line number with no padding
Test (9:1)
9: *
^
';
$this->assertEquals($expected, $actual);
$doubleDigit = new Error(
'Left padded first line number',
null,
new Source("*\n", 'Test', new SourceLocation(9, 1)),
[0]
);
$actual = FormattedError::printError($doubleDigit);
$expected = 'Left padded first line number
Test (9:1)
9: *
^
10:
';
$this->assertEquals($expected, $actual);
}
/**
* @it prints an error with nodes from different sources
*/