symfony - Custom ExceptionListener picks up 403 & 404, but not 500 errors -
exact same problem unanswered question symfony 2.4: why 500 errors not caught kernel.exception listener.
i have implemented custom exception "handler" works awesomely 403 , 404 issues, 500 errors (which want handle, want send email myself system when happens) not trigger custom "handler" , continues behave if custom "handler" not there. code relatively straight forward:
extract app/config/config.yml:
services: core.exceptlistener: class: pmb\licensingbundle\listener\exceptionlistener arguments: ["@service_container", "@router"] tags: - { name: kernel.event_listener, event: kernel.exception, method: onkernelexception, priority: 200 }
entire \pmb\licensingbundle\listener\exceptionlistener.php:
<?php namespace pmb\licensingbundle\listener; use symfony\component\httpkernel\event\getresponseforexceptionevent; use symfony\component\httpfoundation\response; use symfony\component\httpfoundation\redirectresponse; use symfony\component\httpkernel\exception\httpexceptioninterface; use symfony\component\dependencyinjection\containerinterface; use symfony\bundle\twigbundle\twigengine; use sensio\bundle\frameworkextrabundle\configuration\template; class exceptionlistener { private $container; private $router; function __construct($container, $router) { $this->container = $container; $this->router = $router; } public function onkernelexception(getresponseforexceptionevent $event) { $exception = $event->getexception(); die("test"); $request = $this->container->get('request'); $templating = $this->container->get('templating'); if ($exception instanceof \symfony\component\security\core\exception\accessdeniedexception) { # if ajax request, show not error page. if ($request->isxmlhttprequest()) { $response = new response(json_encode(array('error' => 'access denied: not logged in administrator'))); } else { return new redirectresponse($this->router->generate('login')); } $event->setresponse($response); } elseif ($exception->getstatuscode() == 404) { # if ajax request, show not error page. if ($request->isxmlhttprequest()) { $response = new response(json_encode(array('error' => 'requested route not found'))); } else { $response = new response($templating->render('pmblicensingbundle:exception:error404.html.twig', array( 'exception' => $exception ))); } $event->setresponse($response); } else { # todo: send email # if ajax request, not show error page. if ($request->isxmlhttprequest()) $response = new response(json_encode(array('error' => 'internal server error encountered. developer has been notified.'))); else { $response = new response($templating->render('pmblicensingbundle:exception:error500.html.twig', array( 'exception' => $exception ))); } } } }
i put die("test")
in there verify "handler" not being called @ , not problem if-else logic. stated before, works awesomely 404 or 403 errors, 500 errors ignores , behaves in default manner. pretty sure has registering of listener service, cannot find explains how make work properly.
edit: below screenshots of errors not being handled expected. notice on 1 (undefined variable) die("test");
displays @ bottom, on ohter (syntax error) not display, though both seems being caught symfony2. further testing showed die("test");
showed up, die(">> .$exception->getstatuscode()." <<"); did not. assuming causing second exception not seeing, accessdeniedexception did not have function call , had use instanceof
, there many possible errors can come 500 error, how distinguish whether error 1 of these?
further edit: on error did print "test" @ bottom, noticed when $container->testerror();
, not "test", @ bottom of error, when return $container;
, do, though both errors contexterrorexception undefined variable.
the 500 errors got before symfony has chance start. since symfony doesn't start in case cannot handle errors. debug such cases, should @ web server (apache, ngnix, etc) logs.
Comments
Post a Comment