1
votes

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

<?php

namespace App\Exception;

final class ActualiteNotFoundException extends \Exception
{
}

EventSubscriber

<?php
// app/src/EventSubscriber/ActualiteManager.php

namespace 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.yaml
api_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 ?

1
As shown by the only answer, the question was based on a typo. - yivi
Your problem is same as mine, the error handling that is it in the documentation only works if the entity exists in the database. It sadly does not work when entity does not exists at all. I am starting to get pissed of by api-platform, documentation for less advanced users is totally unclear, features you expect to get are missing. I just want a simple thing, if user has language XY then to not return Not found but the translated stuff, and i cant find a way to hook into those messages. - Erik Kubica
I have the same problem, it doesn't work - hous
Does anyone found something ? - Vishal Tanna

1 Answers

0
votes

First of all,

Your are missing ' in this line:

throw new ActualiteNotFoundException(sprintf('The Actualite  does not exist !));

Secondly, You did not change $product into $actualite in this line:

if (!$product->isPubliclyAvailable()) {