Circular reference detected for service "kernel.listener.your_listener_name",

139 views Asked by At

I added the following lines in my services.yml

# default configuration for services in *this* file
_defaults:
    # automatically injects dependencies in your services
    autowire: true
    # automatically registers your services as commands, event subscribers, etc.
    autoconfigure: true
    # this means you cannot fetch services directly from the container via $container->get()
    # if you need to do this, you can override this setting on individual services
    #public: false

When run the following command ./bin/console cach:clear, I received the following error :

  [Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException]                                                                              
  Circular reference detected for service "kernel.listener.your_listener_name", path: "kernel.listener.your_listener_name -> kernel.listener.your_listener_name". 

For information, this is the definition of the service :

kernel.listener.your_listener_name:
    class: DEL\Bundle\ApiBundle\Exception\ApiException
    tags:
        - { name: kernel.event_listener, event: kernel.exception, method: onKernelException }

and here is the service 's class :

<?php

namespace DEL\Bundle\ApiBundle\Exception;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

class ApiException extends HttpException
{
    public function __construct($statusCode = 0, $message = null, \Exception $previous = null, array $headers = array(), $code = 0)
    {
        parent::__construct($statusCode, $message, $previous, $headers, $code);
    }

    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        // You get the exception object from the received event
        $exception = $event->getException();

        if ($exception instanceof APIExceptionInterface) {
            $xml = new \SimpleXMLElement('<xml/>');
            $error = $xml->addChild('error');
            $error->addChild('id', $exception::ID);
            $error->addChild('error', $exception::GLOBAL_ERROR_MESSAGE);
            $error->addChild('message', $exception->getMessage());
            $message = $xml->asXML();
            // Customize your response object to display the exception details
            $response = new Response();
            $response->setContent($message);
            // HttpExceptionInterface is a special type of exception that
            // holds status code and header details
            if ($exception instanceof HttpExceptionInterface) {
                $response->setStatusCode($exception->getStatusCode());
                $response->headers->replace($exception->getHeaders());
            } else {
                $response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
            }
            // Send the modified response object to the event
            $event->setResponse($response);
        }
    }
}

So is there a solution to this problem please ?

0

There are 0 answers