diff --git a/examples/errors_all_options.php b/examples/errors_all_options.php index 4f628cc..e43bf4f 100644 --- a/examples/errors_all_options.php +++ b/examples/errors_all_options.php @@ -16,6 +16,7 @@ // mark the cause of the error $errorSpecApi->blameJsonPointer($pointer='/data/attributes/title'); $errorSpecApi->blameQueryParameter($parameter='filter'); +$errorSpecApi->blameHeader($headerName='X-Foo'); // an identifier useful for helpdesk purposes $errorSpecApi->setUniqueIdentifier($id=42); diff --git a/src/objects/ErrorObject.php b/src/objects/ErrorObject.php index 3d40d78..80120f1 100644 --- a/src/objects/ErrorObject.php +++ b/src/objects/ErrorObject.php @@ -194,6 +194,15 @@ public function blameQueryParameter($parameter) { $this->addSource('parameter', $parameter); } + /** + * blame the header from the request causing this error + * + * @param string $headerName + */ + public function blameHeader($headerName) { + $this->addSource('header', $headerName); + } + /** * @param string $key * @param mixed $value diff --git a/tests/example_output/errors_all_options/errors_all_options.json b/tests/example_output/errors_all_options/errors_all_options.json index 11c8e33..6163dc5 100644 --- a/tests/example_output/errors_all_options/errors_all_options.json +++ b/tests/example_output/errors_all_options/errors_all_options.json @@ -42,7 +42,8 @@ }, "source": { "pointer": "/data/attributes/title", - "parameter": "filter" + "parameter": "filter", + "header": "X-Foo" }, "meta": { "foo": "bar", @@ -63,7 +64,7 @@ "message": "please don't throw things", "code": 500, "file": "/errors_all_options.php", - "line": 30 + "line": 31 } }, { @@ -73,7 +74,7 @@ "message": "something went wrong!", "code": 0, "file": "/errors_all_options.php", - "line": 29 + "line": 30 } }, { diff --git a/tests/example_output/errors_all_options/errors_all_options.php b/tests/example_output/errors_all_options/errors_all_options.php index f0a4076..5c138ce 100644 --- a/tests/example_output/errors_all_options/errors_all_options.php +++ b/tests/example_output/errors_all_options/errors_all_options.php @@ -12,6 +12,7 @@ public static function createJsonapiDocument() { $errorSpecApi = new ErrorObject(); $errorSpecApi->blameJsonPointer($pointer='/data/attributes/title'); $errorSpecApi->blameQueryParameter($parameter='filter'); + $errorSpecApi->blameHeader($headerName='X-Foo'); $errorSpecApi->setUniqueIdentifier($id=42); $errorSpecApi->addMeta($key='foo', $value='bar'); $errorSpecApi->setHttpStatusCode($httpStatusCode=404); diff --git a/tests/objects/ErrorObjectTest.php b/tests/objects/ErrorObjectTest.php index 98a3125..95e6724 100644 --- a/tests/objects/ErrorObjectTest.php +++ b/tests/objects/ErrorObjectTest.php @@ -160,10 +160,18 @@ public function testIsEmpty_All() { $errorObject->addLink('foo', 'https://jsonapi.org'); $this->assertFalse($errorObject->isEmpty()); + $errorObject = new ErrorObject(); + $errorObject->addSource('pointer', '/bar'); + $this->assertFalse($errorObject->isEmpty()); + $errorObject = new ErrorObject(); $errorObject->addSource('parameter', 'bar'); $this->assertFalse($errorObject->isEmpty()); + $errorObject = new ErrorObject(); + $errorObject->addSource('header', 'X-Bar'); + $this->assertFalse($errorObject->isEmpty()); + $errorObject = new ErrorObject(); $errorObject->addMeta('foo', 'bar'); $this->assertFalse($errorObject->isEmpty());