Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/Resources/config/legacy/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<argument type="service" id="api_platform.serializer" />
<argument>%api_platform.error_formats%</argument>
<argument>%api_platform.exception_to_status%</argument>
<argument type="service" id="api_platform.listener.exception" />
<argument type="service" id="api_platform.listener.exception" on-invalid="null" />

<tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" />
</service>
Expand Down
19 changes: 18 additions & 1 deletion src/Symfony/EventListener/ErrorListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use ApiPlatform\Metadata\Error as ErrorOperation;
use ApiPlatform\Metadata\Exception\ProblemExceptionInterface;
use ApiPlatform\Metadata\HttpOperation;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\ResourceClassResolverInterface;
use ApiPlatform\Metadata\Util\ContentNegotiationTrait;
Expand Down Expand Up @@ -111,7 +112,10 @@ protected function duplicateRequest(\Throwable $exception, Request $request): Re
}

if (!$operation->getProvider()) {
$operation = $operation->withProvider(provider: fn () => 'jsonapi' === $format && $errorResource instanceof ConstraintViolationListAwareExceptionInterface ? $errorResource->getConstraintViolationList() : $errorResource);
$data = 'jsonapi' === $format && $errorResource instanceof ConstraintViolationListAwareExceptionInterface ? $errorResource->getConstraintViolationList() : $errorResource;
$dup->attributes->set('_api_error_resource', $data);
$operation = $operation->withExtraProperties(['_api_error_resource' => $data])
->withProvider([self::class, 'provide']);
}

// For our swagger Ui errors
Expand Down Expand Up @@ -216,4 +220,17 @@ private function getFormatOperation(?string $format): string
default => '_api_errors_problem'
};
}

public static function provide(Operation $operation, array $uriVariables = [], array $context = [])
{
if ($data = ($context['request'] ?? null)?->attributes->get('_api_error_resource')) {
return $data;
}

if ($data = $operation->getExtraProperties()['_api_error_resource'] ?? null) {
return $data;
}

throw new \LogicException(sprintf('We could not find the thrown exception in the %s.', self::class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@

/**
* Handles validation errors.
* todo remove this class.
* TODO: remove this class.
*
* @deprecated
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
Expand All @@ -39,9 +41,8 @@ public function __construct(private readonly SerializerInterface $serializer, pr
*/
public function onKernelException(ExceptionEvent $event): void
{
// API Platform 3.2 handles every exception through the exception listener so we just skip this one
if ($this->exceptionListener) {
$this->exceptionListener->onKernelException($event);

return;
}

Expand Down