Med Karim Garali
Med Karim Garali

Reputation: 943

How can I set the response code from the DataPersister

I'm working with Symfony 4.4 / API Platform, and I'm trying to return the response from the DataPersister or to set up its code.

In my DataPersister, I test if the Admin->isManager() is true, So Admin can never be deleted, So in this case I want to return a custom status code in my response 414, and a message "thisAdminIsManager"

AdminDataPersister:

final class AdminDataPersister implements ContextAwareDataPersisterInterface
{
    /* @var EntityManagerInterface */
    private $manager;

    public function __construct(
        EntityManagerInterface $manager
    ){
        $this->manager = $manager;
    }


    public function supports($data, array $context = []): bool
    {
        return $data instanceof Admin;
    }

    public function persist($data, array $context = [])
    {   
        $this->manager->persist($data);
        $this->manager->flush();
    }

    public function remove($data, array $context = [])
    {
        /* @var Admin $data */
        #The Manager can never be deleted:
        if( $data->getManager() ){
            return; //here I want to return the custom response
        }
        $this->manager->remove($data);
        $this->manager->flush();

    }

Upvotes: 0

Views: 1325

Answers (1)

Alexandre Tranchant
Alexandre Tranchant

Reputation: 4570

You should throw an exception, then you should configurate your api_platform to handle this exception. ApiPlatform will convert exception into a response with message and the specified code.

Step1: Create dedicated exception class

<?php
// api/src/Exception/ProductNotFoundException.php

namespace App\Exception;

final class AdminNonDeletableException extends \Exception
{
}

Step2: In your data persister, throw exception:

    public function remove($data, array $context = [])
    {
        /* @var Admin $data */
        #The Manager can never be deleted:
        if( $data->getManager() ){
            throw new AdminNonDeletableException('thisAdminIsManager');
        }
        $this->manager->remove($data);
        $this->manager->flush();

    }

Step3: Add your exception in the config/package/api_platform.yaml file and declare the code number (414)

# 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: !php/const Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST
        ApiPlatform\Core\Exception\FilterValidationException: 400
        Doctrine\ORM\OptimisticLockException: 409

        # Custom mapping
        App\Exception\AdminNonDeletableException: 414 # Here is the handler for your custom exception associated to the 414 code

You can find more information in error handling chapter

Upvotes: 1

Related Questions