如何扩展Symfony 3错误处理?

I'm trying to extend the Symfony 3.3 built-in error handler in order to override what it does on the CLI, but leave the existing behavior in place for web requests.

Whenever I have a bug in my unit tests in Symfony, it spits out 3000 lines of HTML to the CLI. This is just silly.

So I've tried to following the documentation for extending the built in error handling.

I added

services:
    _defaults:
        autowire: true

   twig.exception_listener:
       class:  AppBundle\Controller\MyExceptionController
       public: true
       arguments: "%kernel.debug%"

Like it shows on the docs, and Symfony complains:

Type error: Argument 1 passed to Symfony\Component\DependencyInjection\Definition::setArguments() must be of the type array, string given, called in /Projects/rest_api/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php on line 425

So on another Stack Overflow page, I found you have to also pass in Twig.

Now I have this in services.yml

   twig.exception_listener:
       class:  AppBundle\Controller\MyExceptionController
       public: true
       arguments: ["@twig", "%kernel.debug%"]

So far so good. Here's MyExceptionController:

<?php
// AppBundle/Controllers/MyExceptionController.php

namespace AppBundle\Controller;

use Symfony\Bundle\TwigBundle\Controller\ExceptionController;
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class MyExceptionController extends ExceptionController
{
    /**
     * Converts an Exception to a Response.
     *
     * A "showException" request parameter can be used to force display of an error page (when set to false) or
     * the exception page (when true). If it is not present, the "debug" value passed into the constructor will
     * be used.
     *
     * @param Request $request
     * @param FlattenException $exception
     * @param DebugLoggerInterface|null $logger
     * @return Response
     */
    public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null)
    {
        if (php_sapi_name() === 'cli') {
            $error = <<<EOF
-------------------------------------------------------
--    ______ _____  _____   ____  _____  _ 
--   |  ____|  __ \|  __ \ / __ \|  __ \| |
--   | |__  | |__) | |__) | |  | | |__) | |
--   |  __| |  _  /|  _  /| |  | |  _  /| |
--   | |____| | \ \| | \ \| |__| | | \ \|_|
--   |______|_|  \_\_|  \_\\____/|_|  \_(_)
--                                         
--------------------------------------------------------
EOF;

            $error .= "
".print_r($exception, true)."
";
            return $error;
        } else {
            return parent::showAction($request, $exception, $logger);
        }
    }

}

Here's the weird thing. I get the default HTML error page in the browser, and HTML went away from the CLI, but I'm not seeing my code get called. Even if I put die('Got Here') at the top of my showAction() method, it never is called.

I went back and re-read the docs several times. I tried this:

# app/config/config.yml
twig:
    exception_controller: AppBundle:MyExceptionController:showAction

Still nothing. It seemed to have no effect at all.

So what's going on wrong here?

</div>

Why would you even try rendering twig it CLI? CLI is all about commands, controllers shouldn't be part of it and they can't be because there is no request/response but instead you have stdin and stdout.

If you want to have exception handler for commands then you should use ConsoleEvents::ERROR. But this shouldn't be standard flow of the application, it's better to use try,catch,finally to actually catch exceptions and react accordingly