Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception serializer not been called #2384

Open
GleisonOliveira opened this issue Jan 31, 2023 · 0 comments
Open

Exception serializer not been called #2384

GleisonOliveira opened this issue Jan 31, 2023 · 0 comments

Comments

@GleisonOliveira
Copy link

Hi everyone.

I'm facing a problem with the last version of lib, we are upgrading to symfony 6.2 and version 3.5 of this lib, but the exceptions serializer are not been called anymore.

My config is

fos_rest:
  allowed_methods_listener: true
  service:
    serializer: primepass.fos_serializer
  format_listener:
    rules:
      - { path: '^/resque', priorities: ['text/html', '*/*'], fallback_format: html }
      - { path: ^/, prefer_extension: true, fallback_format: json, priorities: [ json ], }
  exception:
    enabled: true
    map_exception_codes: true
    codes:
      'Primepass\ApiBundle\Exception\ValidateException': 400
      'Primepass\ApiBundle\Exception\NotFoundException': 404
      'Primepass\ApiBundle\Exception\InterNotFoundRequestException': 404
    messages:
      'Primepass\ApiBundle\Exception\NotFoundException': true
      'Primepass\ApiBundle\Exception\ValidateException': true

My serailizer is

<?php

namespace Primepass\ApiBundle\Serializer;

use Doctrine\ORM\EntityManagerInterface;
use FOS\RestBundle\Serializer\SymfonySerializerAdapter;
use Primepass\ApiBundle\Connection\ProviderIntegration\ProviderIntegrationConnection;
use Primepass\ApiBundle\Serializer\FactoryInterface;
use Primepass\ApiBundle\Serializer\Normalizer;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Serializer;

class FosSerializerAdapterFactory implements FactoryInterface
{
    /**
     * @var EntityManagerInterface
     */
    private static $em;

    /**
     * @var ProviderIntegrationConnection
     */
    private static $providerIntegrationConnection;

    /**
     * @param EntityManagerInterface $em
     * @param ProviderIntegrationConnection $providerIntegrationConnection
     * @return SymfonySerializerAdapter
     */
    public static function create(EntityManagerInterface $em, ProviderIntegrationConnection $providerIntegrationConnection): SymfonySerializerAdapter
    {
        self::$em = $em;
        self::$providerIntegrationConnection = $providerIntegrationConnection;

        $serializer = new Serializer(
            self::getNormalizers(),
            self::getEncoders()
        );

        return new SymfonySerializerAdapter($serializer);
    }

    /**
     * @return array
     */
    private static function getNormalizers()
    {
        return [
            new Normalizer\ExceptionNormalize,
        ];
    }

    /**
     * @return array
     */
    private static function getEncoders()
    {
        return [new JsonEncoder];
    }
}

and my normalizer is

<?php

namespace Primepass\ApiBundle\Serializer\Normalizer;

use Primepass\ApiBundle\Exception\AccessDeniedException;
use Primepass\ApiBundle\Exception\NotFoundException;
use Primepass\ApiBundle\Exception\ValidateException;
use Primepass\ApiBundle\Exception\InvalidRequestException;
use Primepass\ApiBundle\Exception\RateLimitException;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\SerializerAwareInterface;
use Symfony\Component\Serializer\SerializerAwareTrait;
use Symfony\Component\ErrorHandler\Error\FatalError;

class ExceptionNormalize implements
    NormalizerInterface,
    SerializerAwareInterface
{
    use SerializerAwareTrait;

    /**
     * @param object $object
     * @param string $format
     * @param array $context
     *
     * @return array
     */
    public function normalize($object, $format = null, array $context = array()): array
    {
        return [
            'error' => [
                'code' => $object->getExceptionError(),
                'message' => $object->getExceptionMessage(),
                'details' => $object->getValidateErrors()
            ]
        ];
    }

    /**
     *
     * @param mixed $data
     * @param string $format
     *
     * @return boolean
     */
    public function supportsNormalization($data, $format = null)
    {
        return (
            $data instanceof ValidateException ||
            $data instanceof NotFoundException ||
            $data instanceof InvalidRequestException ||
            $data instanceof AccessDeniedException ||
            $data instanceof FatalError || 
            $data instanceof RateLimitException
        );
    }
}

and my serializer service are registered

  primepass.fos_serializer:
    class: 'FOS\RestBundle\Serializer\SymfonySerializerAdapter'
    factory: [Primepass\ApiBundle\Serializer\FosSerializerAdapterFactory, create]

This code was working well in version 2.4.

There is any suggestion to how to solve this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant