0

In an attempt to combine Monolog e-mailing errors with a custom exception handler I get the following:

[Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException] Circular reference detected for service "router", path: "router -> monolog.logger.router -> monolog.handler.grouped -> mana.exception.listener -> templating -> twig -> templating.helper.logout_url".

I make no claims to really know what I'm doing here, as evidenced by all the errors.

Services excerpt:

  mana.exception.listener:
    class:  Mana\ClientBundle\EventListener\ExceptionListener
    arguments: [@templating, @kernel]
    tags:
      - { name: kernel.event_listener, event: kernel.exception, method: onKernelException }

Config excerpt:

monolog:
    handlers:
        main:
            type:  stream
            path:  %kernel.logs_dir%/%kernel.environment%.log
            level: debug
            handler: grouped
        grouped:
            type: group
            members: [mail, custom]
        mail:
            type:         fingers_crossed
            action_level: error
            handler:      buffered
        buffered:
            type:    buffer
            handler: swift
        swift:
            type:       swift_mailer
            from_email: error@projectmana.org
            to_email:   truckeetrout@yahoo.com
            subject:    An Error Occurred!
            level:      debug
        custom:
            type: service
            id: mana.exception.listener

Custom handler:

class ExceptionListener {

    protected $templating;
    protected $kernel;

    public function __construct(EngineInterface $templating, $kernel) {
        $this->templating = $templating;
        $this->kernel = $kernel;
    }

    public function onKernelException(GetResponseForExceptionEvent $event) {
        // provide the better way to display a enhanced error page only in prod environment, if you want
        if ('prod' == $this->kernel->getEnvironment()) {
            // exception object
            $exception = $event->getException();

            // new Response object
            $response = new Response();

            // set response content
            $response->setContent(
                    // create you custom template AcmeFooBundle:Exception:exception.html.twig
                    $this->templating->render(
                            'ManaClientBundle:Exception:exception.html.twig', array('exception' => $exception)
                    )
            );

            // 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(500);
            }

            // set the new $response object to the $event
            $event->setResponse($response);
        }
    }
geoB
  • 4,578
  • 5
  • 37
  • 70

2 Answers2

0

It's your Exception Listener. Delete @kernel from the arguments array and you're fine

mana.exception.listener:
    class:  Mana\ClientBundle\EventListener\ExceptionListener
    arguments: [@templating]
    tags:
      - { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
Pi Wi
  • 1,076
  • 1
  • 11
  • 20
  • Afraid that is not so. Eliminating `@kernel` from did not eliminate the circular reference error. – geoB Nov 17 '13 at 23:07
  • It could also be your templating, but I don't think so. Did you read http://symfony.com/doc/current/cookbook/service_container/event_listener.html ? – Pi Wi Nov 18 '13 at 07:51
0

Your custom exception listener probably depends on monolog either in the templating or kernel service.

Leevi Graham
  • 654
  • 8
  • 19