Skip to content

Commit

Permalink
self review
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed May 22, 2023
1 parent b2ef776 commit c24eb36
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/ApiResource/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
operations: [
new Get(name: '_api_errors_hydra', outputFormats: ['jsonld' => ['application/ld+json']], normalizationContext: ['groups' => ['jsonld'], 'skip_null_values' => true]),
new Get(name: '_api_errors_problem', outputFormats: ['json' => ['application/problem+json']], normalizationContext: ['groups' => ['jsonproblem'], 'skip_null_values' => true]),
new Get(name: '_api_errors_jsonapi', outputFormats: ['jsonapi' => ['application/vnd.api+json']], normalizationContext: ['groups' => ['jsonapi'], 'skip_null_values' => true]),
new Get(name: '_api_errors_jsonapi', outputFormats: ['jsonapi' => ['application/vnd.api+json']], normalizationContext: ['groups' => ['jsonapi'], 'skip_null_values' => true], provider: 'api_platform.state_provider.json_api.default_error'),
]
)]
class Error extends \Exception implements ProblemExceptionInterface, HttpExceptionInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

use ApiPlatform\Metadata\Resource\ResourceNameCollection;

/**
* @internal
*/
final class ClassNameResourceNameCollectionFactory implements ResourceNameCollectionFactoryInterface
{
/**
Expand Down
14 changes: 5 additions & 9 deletions src/State/DefaultErrorProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,14 @@
namespace ApiPlatform\State;

use ApiPlatform\Metadata\Operation;
use ApiPlatform\Symfony\Validator\Exception\ConstraintViolationListAwareExceptionInterface;

class DefaultErrorProvider implements ProviderInterface
/**
* @internal
*/
final class DefaultErrorProvider implements ProviderInterface
{
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object
{
$exception = $context['previous_data'];

if ($exception instanceof ConstraintViolationListAwareExceptionInterface && '_api_validation_errors_jsonapi' === $context['operation_name']) {
return $exception->getConstraintViolationList();
}

return $exception;
return $context['previous_data'];
}
}
34 changes: 34 additions & 0 deletions src/State/JsonApi/DefaultErrorProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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\State\JsonApi;

use ApiPlatform\Metadata\Operation;
use ApiPlatform\Symfony\Validator\Exception\ConstraintViolationListAwareExceptionInterface;

/**
* @internal
*/
final class DefaultErrorProvider implements ProviderInterface
{
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object
{
$exception = $context['previous_data'];

if ($exception instanceof ConstraintViolationListAwareExceptionInterface) {
return $exception->getConstraintViolationList();
}

return $exception;
}
}
5 changes: 5 additions & 0 deletions src/Symfony/Bundle/Resources/config/state.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@
<tag name="api_platform.state_provider" key="api_platform.state_provider.default_error" />
</service>

<service id="api_platform.state_provider.json_api.default_error" class="ApiPlatform\State\JsonApi\DefaultErrorProvider" >
<tag name="api_platform.state_provider" key="ApiPlatform\State\JsonApi\DefaultErrorProvider" />
<tag name="api_platform.state_provider" key="api_platform.state_provider.jsonapi.default_error" />
</service>

<service id="ApiPlatform\State\ObjectProvider" alias="api_platform.state_provider.object" />
</services>
</container>

0 comments on commit c24eb36

Please sign in to comment.