Skip to content

Commit 516ec31

Browse files
committed
Error handling for invalid JSON body
1 parent 8fea011 commit 516ec31

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

src/Context.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
use ArrayObject;
66
use HttpAccept\AcceptParser;
7+
use JsonException;
78
use Nyholm\Psr7\Response;
89
use Nyholm\Psr7\Stream;
910
use Psr\Http\Message\ResponseInterface;
1011
use Psr\Http\Message\ServerRequestInterface;
1112
use RuntimeException;
13+
use Tobyz\JsonApiServer\Exception\Data\InvalidJsonException;
1214
use Tobyz\JsonApiServer\Exception\ErrorProvider;
1315
use Tobyz\JsonApiServer\Exception\Field\InvalidFieldValueException;
1416
use Tobyz\JsonApiServer\Exception\JsonApiErrorsException;
@@ -122,9 +124,17 @@ public function currentUrl(array $queryParams = []): string
122124
*/
123125
public function body(): ?array
124126
{
125-
return $this->body ??=
126-
(array) $this->request->getParsedBody() ?:
127-
json_decode($this->request->getBody()->getContents(), true);
127+
try {
128+
return $this->body ??=
129+
(array) $this->request->getParsedBody() ?:
130+
json_decode(
131+
$this->request->getBody()->getContents(),
132+
true,
133+
flags: JSON_THROW_ON_ERROR,
134+
);
135+
} catch (JsonException $e) {
136+
throw new InvalidJsonException($e->getMessage());
137+
}
128138
}
129139

130140
public function id(Resource $resource, $model): string
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Tobyz\JsonApiServer\Exception\Data;
4+
5+
use Tobyz\JsonApiServer\Exception\BadRequestException;
6+
7+
class InvalidJsonException extends BadRequestException
8+
{
9+
}

tests/specification/CreatingResourcesTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace Tobyz\Tests\JsonApiServer\specification;
44

5+
use Nyholm\Psr7\Stream;
56
use Tobyz\JsonApiServer\Endpoint\Create;
67
use Tobyz\JsonApiServer\Endpoint\Show;
78
use Tobyz\JsonApiServer\Exception\BadRequestException;
89
use Tobyz\JsonApiServer\Exception\ConflictException;
10+
use Tobyz\JsonApiServer\Exception\Data\InvalidJsonException;
911
use Tobyz\JsonApiServer\Exception\ForbiddenException;
1012
use Tobyz\JsonApiServer\Exception\ResourceNotFoundException;
1113
use Tobyz\JsonApiServer\JsonApi;
@@ -37,6 +39,15 @@ public function setUp(): void
3739
$this->api->resource(new MockResource('pets'));
3840
}
3941

42+
public function test_bad_request_error_if_body_is_invalid_json()
43+
{
44+
$this->expectException(InvalidJsonException::class);
45+
46+
$this->api->handle(
47+
$this->buildRequest('POST', '/users')->withBody(Stream::create('invalid json')),
48+
);
49+
}
50+
4051
public function test_bad_request_error_if_body_does_not_contain_data_type()
4152
{
4253
$this->expectException(BadRequestException::class);

0 commit comments

Comments
 (0)