When I want to get item with URL http://127.0.0.1:8000/actualites/1 it throw exception "hydra:description": "Not Found". I tried to custom this exception with Errors Handling - API Platform: Documentation, nothing change!!
app/src/Exception/ActualiteNotFoundException.php
<?phpnamespace App\Exception;final class ActualiteNotFoundException extends \Exception{}
EventSubscriber
<?php// app/src/EventSubscriber/ActualiteManager.phpnamespace App\EventSubscriber;use ApiPlatform\Core\EventListener\EventPriorities;use App\Entity\Actualite;use App\Exception\ActualiteNotFoundException;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;use Symfony\Component\HttpKernel\KernelEvents;final class ActualiteManager implements EventSubscriberInterface{ public static function getSubscribedEvents(): array { return [ KernelEvents::VIEW => ['checkProductAvailability', EventPriorities::PRE_VALIDATE], ]; } public function checkProductAvailability(GetResponseForControllerResultEvent $event): void { $actualite= $event->getControllerResult(); if (!$actualite instanceof Actualite|| !$event->getRequest()->isMethodSafe(false)) { return; } if (!$product->isPubliclyAvailable()) { // Using internal codes for a better understanding of what's going on throw new ActualiteNotFoundException(sprintf('The Actualite does not exist !)); } }}
config
# config/packages/api_platform.yamlapi_platform: # ... exception_to_status: # The 4 following handlers are registered by default, keep those lines to prevent unexpected side effects Symfony\Component\Serializer\Exception\ExceptionInterface: 400 # Use a raw status code (recommended) ApiPlatform\Core\Exception\InvalidArgumentException: 'HTTP_BAD_REQUEST' # Or a `Symfony\Component\HttpFoundation\Response`'s constant ApiPlatform\Core\Exception\FilterValidationException: 400 Doctrine\ORM\OptimisticLockException: 409 # Custom mapping App\Exception\ActualiteNotFoundException: 404 # Here is the handler for our custom exception
How can i custom the exception 404 not found ?