Skip to content

Commit

Permalink
feat(symfony): skip error handler (#6463)
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka authored Jul 10, 2024
1 parent 26d5cbb commit 7399fcf
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/Symfony/EventListener/ErrorListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected function duplicateRequest(\Throwable $exception, Request $request): Re
// - use api platform to handle errors (the default behavior we handle firewall errors for example but they're out of our scope)

// Let the error handler take this we don't handle HTML nor non-api platform requests
if ('html' === $format) {
if (false === ($apiOperation?->getExtraProperties()['_api_error_handler'] ?? true) || 'html' === $format) {
$this->controller = 'error_controller';

return parent::duplicateRequest($exception, $request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Tests\Fixtures\TestBundle\Exception\TestException;

#[Get(uriTemplate: 'issue5921{._format}', read: true, provider: [ExceptionResource::class, 'provide'], extraProperties: ['rfc_7807_compliant_errors' => false])]
#[Get(
uriTemplate: 'issue5921{._format}',
read: true,
provider: [ExceptionResource::class, 'provide'],
extraProperties: ['_api_error_handler' => false]
)]
class ExceptionResource
{
public static function provide(Operation $operation, array $uriVariables = [], array $context = []): void
Expand Down
21 changes: 9 additions & 12 deletions tests/Fixtures/TestBundle/Serializer/ErrorNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace ApiPlatform\Tests\Fixtures\TestBundle\Serializer;

use ApiPlatform\Tests\Fixtures\TestBundle\Exception\TestException;
use Symfony\Component\ErrorHandler\Exception\FlattenException;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

final class ErrorNormalizer implements NormalizerInterface
Expand All @@ -32,20 +32,17 @@ public function normalize(mixed $object, ?string $format = null, array $context

public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
{
if (\is_object($data) && $data instanceof TestException) {
return true;
}

return $this->decorated->supportsNormalization($data, $format, $context);
}

public function hasCacheableSupportsMethod(): bool
{
return false;
return 'json' === $format;
}

public function getSupportedTypes(?string $format): array
{
return $this->decorated->getSupportedTypes($format);
if ('json' === $format) {
return [
FlattenException::class => true,
];
}

return [];
}
}
5 changes: 3 additions & 2 deletions tests/Fixtures/app/config/config_common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,9 @@ services:
$decorated: '@.inner'

ApiPlatform\Tests\Fixtures\TestBundle\Serializer\ErrorNormalizer:
decorates: 'api_platform.problem.normalizer.error'
arguments: [ '@.inner' ]
arguments: [ '@serializer.normalizer.problem' ]
tags:
- name: 'serializer.normalizer'

api_platform.http_cache.tag_collector:
class: ApiPlatform\Tests\Fixtures\TestBundle\HttpCache\TagCollectorDefault
Expand Down

0 comments on commit 7399fcf

Please sign in to comment.