Skip to content

Commit 8408dec

Browse files
committedJan 21, 2023
the exception can be called with null $entity
1 parent 21e23f3 commit 8408dec

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed
 

‎src/Error/Exception/JsonApiException.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@ class JsonApiException extends BadRequestException
99
{
1010
protected $requestErrors;
1111

12-
public function __construct(EntityInterface|array $entity, $message = null, $code = 400)
12+
public function __construct(EntityInterface|array|null $entity, $message = null, $code = 400)
1313
{
14+
if (is_null($entity)) {
15+
$this->requestErrors = [];
16+
}
1417
if (is_array($entity)) {
1518
foreach ($entity as $ent) {
1619
$this->requestErrors[] = $ent->getErrors();
1720
}
1821
}
19-
if (!is_array($entity)) {
22+
if ($entity instanceof EntityInterface) {
2023
$this->requestErrors = $entity->getErrors();
2124
}
2225

‎tests/TestCase/JsonApiExceptionRendererTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,19 @@ public function testJsonApiWithErrorCode()
7979

8080
$this->assertEquals(406, $response->getStatusCode());
8181
}
82+
83+
public function testJsonApiWithNull()
84+
{
85+
$message = 'Response on null';
86+
$exception = new JsonApiException(null, $message);
87+
88+
$request = (new ServerRequest())
89+
->withParam('controller', 'Foo')
90+
->withParam('action', 'bar');
91+
$exceptionRenderer = new JsonApiExceptionRenderer($exception, $request);
92+
93+
$response = $exceptionRenderer->render();
94+
$responseBody = json_decode($response->__toString());
95+
$this->assertEquals($message, $responseBody->message);
96+
}
8297
}

0 commit comments

Comments
 (0)