Nelson Teixeira
Nelson Teixeira

Reputation: 6562

Symfony 2.7 custom exception handler

I'm trying to use a custom controller for exception handling in my Symfony 2.7 project, but I'getting this error: Type error: Too few arguments to function Symfony\Bundle\TwigBundle\Controller\ExceptionController::__construct()

I have set the exception class like this:

twig:
    exception_controller: MyProject\MainBundle\Controller\MyProjectExceptionController::showException 

I know I have to configure a service to pass the correct arguments to the constructor. So I have the follwing configuration in config.yml:

services:
    myproject.twig.controller.exception: 
        class: MyProject\MainBundle\Controller\MyProjectExceptionController 
        arguments: [@twig, %kernel.debug%]

I have read many SO's answers on the subject and seen that there is some changes in this configuration between Symfony's versions. I've tried also app.configuration_exception and some other options.

Anyone knows what is the correct configuration specifically for Symfony 2.7 ?

Edit

Answering @Constantin:

Actually I was still doing initial tests. So I basically copied Twig's ExceptionControler, and stripped all but showAction and included a test environment variable in order to see if everything was working fine. This is the result:

<?php

namespace MyProject\MainBundle\Controller;

use Symfony\Bundle\TwigBundle\Controller\ExceptionController;

use Symfony\Component\HttpKernel\Exception\FlattenException;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
//use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
//use Symfony\Component\Templating\TemplateReferenceInterface;
//use Twig\Environment;
//use Twig\Error\LoaderError;
//use Twig\Loader\ExistsLoaderInterface;

class MyProjectExceptionController extends ExceptionController {

    public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null)
    {
        $currentContent = $this->getAndCleanOutputBuffering($request->headers->get('X-Php-Ob-Level', -1));
        $showException = $request->attributes->get('showException', $this->debug); // As opposed to an additional parameter, this maintains BC

        $code = $exception->getStatusCode();

        return new Response($this->twig->render(
            (string) $this->findTemplate($request, $request->getRequestFormat(), $code, $showException),
            array(
                'status_code' => $code,
                'status_text' => isset(Response::$statusTexts[$code]) ? Response::$statusTexts[$code] : '',
                'exception' => $exception,
                'logger' => $logger,
                'currentContent' => $currentContent,
                'test' => "TEST"
            )
            ), 200, array('Content-Type' => $request->getMimeType($request->getRequestFormat()) ?: 'text/html'));
    }
}

Upvotes: 0

Views: 429

Answers (1)

Constantin
Constantin

Reputation: 1304

I see that you have a little mistake in

twig:
    exception_controller: MyProject\MainBundle\Controller\MyProjectExceptionController::showException

That line should be "showAction" and not "showException". But I cannot confirm that it will solve the issue.

As you are extending "ExceptionController", the definition of the service should be fine and will match the constructor. But it is better to put services definitions in services.yml

Can you run this command and try to find your service in it (to be sure it's there) :

php app/console debug:container

[EDIT] Ok just read the doc and we can read :

In case of extending the ExceptionController you may configure a service to pass the Twig environment and the debug flag to the constructor. *** And then configure twig.exception_controller using the controller as services syntax (e.g. app.exception_controller:showAction).

In your case it would give:

twig:
    exception_controller: myproject.twig.controller.exception:showAction

I guess this one should solve your issue... Please try and let me know the result.

PS: doc is available here

Upvotes: 1

Related Questions