From f4008f0fb2294178fc0ecc3f7a7f71a13b543db1 Mon Sep 17 00:00:00 2001 From: Vladimir Razuvaev Date: Tue, 7 Aug 2018 23:20:39 +0700 Subject: [PATCH] Error formatting: display error extensions under `extensions` key --- CHANGELOG.md | 20 +++++++++++++++++ UPGRADE.md | 37 ++++++++++++++++++++++++++++++++ src/Error/Error.php | 7 +++--- src/Error/FormattedError.php | 8 +++---- tests/Error/ErrorTest.php | 5 ++++- tests/Error/PrintErrorTest.php | 39 ++++++++++++++++++++++++++++++++++ 6 files changed, 106 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc7a1b2..100a1db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/UPGRADE.md b/UPGRADE.md index c9e2443..40b9d72 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -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 diff --git a/src/Error/Error.php b/src/Error/Error.php index 1060161..02c26ec 100644 --- a/src/Error/Error.php +++ b/src/Error/Error.php @@ -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; } diff --git a/src/Error/FormattedError.php b/src/Error/FormattedError.php index b73e641..25d364d 100644 --- a/src/Error/FormattedError.php +++ b/src/Error/FormattedError.php @@ -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) { diff --git a/tests/Error/ErrorTest.php b/tests/Error/ErrorTest.php index 8c98e7b..03abbad 100644 --- a/tests/Error/ErrorTest.php +++ b/tests/Error/ErrorTest.php @@ -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()); } } diff --git a/tests/Error/PrintErrorTest.php b/tests/Error/PrintErrorTest.php index acb2428..8cff56c 100644 --- a/tests/Error/PrintErrorTest.php +++ b/tests/Error/PrintErrorTest.php @@ -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 */