Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions features/jsonapi/errors.feature
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,13 @@ Feature: JSON API error handling
And the JSON node "errors[0].status" should be equal to 404
And the JSON node "errors[0].detail" should exist
And the JSON node "errors[0].type" should exist

Scenario: Get a proper error when ItemNotFoundException is thrown from a provider
When I send a "GET" request to "/jsonapi_error_test/nonexistent"
Then the response status code should be 404
And the response should be in JSON
And the header "Content-Type" should be equal to "application/vnd.api+json; charset=utf-8"
And the JSON node "errors" should exist
And the JSON node "errors[0].status" should exist
And the JSON node "errors[0].title" should exist
And the JSON node "errors[0].id" should exist
9 changes: 9 additions & 0 deletions src/JsonApi/Serializer/ErrorNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ public function __construct(private ?NormalizerInterface $itemNormalizer = null)
public function normalize(mixed $object, ?string $format = null, array $context = []): array
{
$jsonApiObject = $this->itemNormalizer->normalize($object, $format, $context);

if (!isset($jsonApiObject['data']['attributes'])) {
return ['errors' => [[
'id' => $jsonApiObject['data']['id'] ?? uniqid('error_', true),
'status' => (string) (method_exists($object, 'getStatusCode') ? $object->getStatusCode() : 500),
'title' => method_exists($object, 'getMessage') ? $object->getMessage() : 'An error occurred',
]]];
}

$error = $jsonApiObject['data']['attributes'];
$error['id'] = $jsonApiObject['data']['id'];
if (isset($error['type'])) {
Expand Down
36 changes: 36 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/JsonApiErrorTestResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Tests\Fixtures\TestBundle\State\JsonApiErrorTestProvider;

#[ApiResource(
operations: [
new Get(
uriTemplate: '/jsonapi_error_test/{id}',
provider: JsonApiErrorTestProvider::class,
),
],
formats: ['jsonapi' => ['application/vnd.api+json']],
)]
class JsonApiErrorTestResource
{
#[ApiProperty(identifier: true)]
public string $id;

public string $name;
}
37 changes: 37 additions & 0 deletions tests/Fixtures/TestBundle/State/JsonApiErrorTestProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Fixtures\TestBundle\State;

use ApiPlatform\Metadata\Exception\ItemNotFoundException;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\JsonApiErrorTestResource;

class JsonApiErrorTestProvider implements ProviderInterface
{
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
$id = $uriVariables['id'] ?? null;

if ('existing' === $id) {
$resource = new JsonApiErrorTestResource();
$resource->id = $id;
$resource->name = 'Existing Resource';

return $resource;
}

throw new ItemNotFoundException(\sprintf('Resource "%s" not found.', $id));
}
}
Loading