8

Accordingly to this doc page:

http://symfony.com/doc/current/cookbook/controller/error_pages.html

Symfony uses the TwigBundle to control the display of exceptions. However, i'm not looking to customize the display, as mentioned in the doc, i'm looking to override this. I'm working on a small REST API and i wanted to override the calling of TwigBundle to my bundle, making my own exceptions handling (in terms of REST: map correct HTTP status codes and plain-text body responses).

I couldn't find anything about this and the reference on the manual is not that good, specially on the kernel part. Maybe someone already did this and can help me out? Thanks.

2 Answers 2

14

You should create a listener that listens on kernel.exception event. In onKernelException method of that listener you can check for your exception e.g

On exception listener class

  //namespace declarations
  class YourExceptionListener
  {

      public function onKernelException(GetResponseForExceptionEvent $event)
      {
        $exception =  $event->getException();
        if ($exception instanceof YourException) {
            //create response, set status code etc.
            $event->setResponse($response); //event will stop propagating here. Will not call other listeners.
        }
      }
  }

The service declaration would be

 //services.yml
 kernel.listener.yourlisener:
  class: FQCN\Of\YourExceptionListener
  tags:
    - { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
Sign up to request clarification or add additional context in comments.

3 Comments

Can i place the listener on the root of my bundle?
In debug mode dev toolbar profiler page. Select events tab on left side. You will see list of listeners. Check if your listener is there.
My bad, i was using the services.xml and forgot to change my Extension.
1

Bellow is part of my AppKernel.php for disabling internal Exception catch by Symfony for JSON requests, (you can override handle method instead of creating second one)

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;

class AppKernel extends Kernel {
  public function init() {
    parent::init();

    if ($this->debug) {
      // workaround for nasty PHP BUG when E_STRICT errors are reported
      error_reporting(E_ALL);
    }
  }

  public function handleForJson(Request $request,
                                $type = HttpKernelInterface::MASTER_REQUEST,
                                $catch = true
  ) {
    return parent::handle($request, $type, false);
  }
  ...

3 Comments

This seems to be a little hackish... any other way?
Extending Class with OOP convetion is hackish?
Not sure what you mean by "OOP convention". Good idea is to not to overwrite framework classes. KernelEvents::EXCEPTION is thrown on very beginning of handleResponse so you can plug in there by listening for it in your own listener instead of modifying framework classes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.