Skip to content

Commit e771697

Browse files
committed
Add error ID member support
1 parent 23b8608 commit e771697

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ and this project adheres to
3737
- Add `Resource::links()` method for defining custom resource-level links
3838
(including `describedby`)
3939
- Add `Schema\Link` class for defining rich link objects with metadata
40+
- Add `JsonApiError::id(string $id)` method for setting error IDs
4041
- Add full support for JSON:API profiles:
4142
- Parse profile URIs from `Accept` header
4243
- `Context::profileRequested(string $uri): bool` - check if a profile was

src/Exception/Concerns/JsonApiError.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ public function links(array $links): static
4848
return $this;
4949
}
5050

51+
public function id(string $id): static
52+
{
53+
$this->error['id'] = $id;
54+
55+
return $this;
56+
}
57+
5158
public function getJsonApiError(): array
5259
{
5360
$class = (new ReflectionClass($this))->getShortName();

tests/feature/ErrorTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,44 @@ public function test_multiple_errors_most_applicable_status()
154154

155155
$this->assertEquals(500, $response->getStatusCode());
156156
}
157+
158+
public function test_error_id_can_be_set_via_fluent_method()
159+
{
160+
$response = $this->api->error((new MockErrorException())->id('error-12345'));
161+
162+
$this->assertJsonApiDocumentSubset(
163+
[
164+
'errors' => [
165+
[
166+
'id' => 'error-12345',
167+
'status' => '400',
168+
],
169+
],
170+
],
171+
$response->getBody(),
172+
);
173+
}
174+
175+
public function test_error_id_can_be_set_via_customization()
176+
{
177+
$this->api->errors([
178+
MockErrorException::class => [
179+
'id' => 'custom-error-id',
180+
],
181+
]);
182+
183+
$response = $this->api->error(new MockErrorException());
184+
185+
$this->assertJsonApiDocumentSubset(
186+
[
187+
'errors' => [
188+
[
189+
'id' => 'custom-error-id',
190+
'status' => '400',
191+
],
192+
],
193+
],
194+
$response->getBody(),
195+
);
196+
}
157197
}

0 commit comments

Comments
 (0)