Skip to content

Commit

Permalink
the exception can be called with null $entity
Browse files Browse the repository at this point in the history
  • Loading branch information
rrd108 committed Jan 21, 2023
1 parent 21e23f3 commit 8408dec
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Error/Exception/JsonApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ class JsonApiException extends BadRequestException
{
protected $requestErrors;

public function __construct(EntityInterface|array $entity, $message = null, $code = 400)
public function __construct(EntityInterface|array|null $entity, $message = null, $code = 400)
{
if (is_null($entity)) {
$this->requestErrors = [];
}
if (is_array($entity)) {
foreach ($entity as $ent) {
$this->requestErrors[] = $ent->getErrors();
}
}
if (!is_array($entity)) {
if ($entity instanceof EntityInterface) {
$this->requestErrors = $entity->getErrors();
}

Expand Down
15 changes: 15 additions & 0 deletions tests/TestCase/JsonApiExceptionRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,19 @@ public function testJsonApiWithErrorCode()

$this->assertEquals(406, $response->getStatusCode());
}

public function testJsonApiWithNull()
{
$message = 'Response on null';
$exception = new JsonApiException(null, $message);

$request = (new ServerRequest())
->withParam('controller', 'Foo')
->withParam('action', 'bar');
$exceptionRenderer = new JsonApiExceptionRenderer($exception, $request);

$response = $exceptionRenderer->render();
$responseBody = json_decode($response->__toString());
$this->assertEquals($message, $responseBody->message);
}
}

0 comments on commit 8408dec

Please sign in to comment.