Skip to content

Commit 346c4be

Browse files
committed
Merge branch 'blame-headers-for-errors' into 2.4.0
2 parents 6afd836 + d9c378d commit 346c4be

File tree

5 files changed

+23
-3
lines changed

5 files changed

+23
-3
lines changed

examples/errors_all_options.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// mark the cause of the error
1717
$errorSpecApi->blameJsonPointer($pointer='/data/attributes/title');
1818
$errorSpecApi->blameQueryParameter($parameter='filter');
19+
$errorSpecApi->blameHeader($headerName='X-Foo');
1920

2021
// an identifier useful for helpdesk purposes
2122
$errorSpecApi->setUniqueIdentifier($id=42);

src/objects/ErrorObject.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,15 @@ public function blameQueryParameter($parameter) {
194194
$this->addSource('parameter', $parameter);
195195
}
196196

197+
/**
198+
* blame the header from the request causing this error
199+
*
200+
* @param string $headerName
201+
*/
202+
public function blameHeader($headerName) {
203+
$this->addSource('header', $headerName);
204+
}
205+
197206
/**
198207
* @param string $key
199208
* @param mixed $value

tests/example_output/errors_all_options/errors_all_options.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
},
4343
"source": {
4444
"pointer": "/data/attributes/title",
45-
"parameter": "filter"
45+
"parameter": "filter",
46+
"header": "X-Foo"
4647
},
4748
"meta": {
4849
"foo": "bar",
@@ -63,7 +64,7 @@
6364
"message": "please don't throw things",
6465
"code": 500,
6566
"file": "/errors_all_options.php",
66-
"line": 30
67+
"line": 31
6768
}
6869
},
6970
{
@@ -73,7 +74,7 @@
7374
"message": "something went wrong!",
7475
"code": 0,
7576
"file": "/errors_all_options.php",
76-
"line": 29
77+
"line": 30
7778
}
7879
},
7980
{

tests/example_output/errors_all_options/errors_all_options.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public static function createJsonapiDocument() {
1212
$errorSpecApi = new ErrorObject();
1313
$errorSpecApi->blameJsonPointer($pointer='/data/attributes/title');
1414
$errorSpecApi->blameQueryParameter($parameter='filter');
15+
$errorSpecApi->blameHeader($headerName='X-Foo');
1516
$errorSpecApi->setUniqueIdentifier($id=42);
1617
$errorSpecApi->addMeta($key='foo', $value='bar');
1718
$errorSpecApi->setHttpStatusCode($httpStatusCode=404);

tests/objects/ErrorObjectTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,18 @@ public function testIsEmpty_All() {
160160
$errorObject->addLink('foo', 'https://jsonapi.org');
161161
$this->assertFalse($errorObject->isEmpty());
162162

163+
$errorObject = new ErrorObject();
164+
$errorObject->addSource('pointer', '/bar');
165+
$this->assertFalse($errorObject->isEmpty());
166+
163167
$errorObject = new ErrorObject();
164168
$errorObject->addSource('parameter', 'bar');
165169
$this->assertFalse($errorObject->isEmpty());
166170

171+
$errorObject = new ErrorObject();
172+
$errorObject->addSource('header', 'X-Bar');
173+
$this->assertFalse($errorObject->isEmpty());
174+
167175
$errorObject = new ErrorObject();
168176
$errorObject->addMeta('foo', 'bar');
169177
$this->assertFalse($errorObject->isEmpty());

0 commit comments

Comments
 (0)