0

As it's not polite to answer in a another topic I ask the question related this this topic here again:

How to log ZF2 controller exceptions

There is stated that you can log any Uncaught Exceptions in this way, but isn't it that Uncaught Exceptions exit the the further running of the webserver done by PHP ? I wonder what is ment here or tried to be done.

public function onBootstrap(MvcEvent $e)
    {
        $eventManager = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);
        /**
         * Log any Uncaught Exceptions, including all Exceptions in the stack
         */
        $sharedManager = $e->getApplication()->getEventManager()->getSharedManager();
        $sm = $e->getApplication()->getServiceManager();
        $sharedManager->attach('Zend\Mvc\Application', 'dispatch.error',
            function($e) use ($sm) {
                if ($e->getParam('exception')){
                    $ex = $e->getParam('exception');
                    do {
                        $sm->get('Logger')->crit(
                            sprintf(
                               "%s:%d %s (%d) [%s]\n", 
                                $ex->getFile(), 
                                $ex->getLine(), 
                                $ex->getMessage(), 
                                $ex->getCode(), 
                                get_class($ex)
                            )
                        );
                    }
                    while($ex = $ex->getPrevious());
                }
            }
        );
Fabb
  • 21
  • 5

1 Answers1

0

isn't it that Uncaught Exceptions exit the the further running of the webserver done by PHP

No, uncaught exception only stop processing current request under standard PHP setup, be that mod-php or fpm or cgi. Zend\Mvc though catches all exception during dispatch, and makes them available to 'dispatch.error' handler (the code you posted is such a handler, that logs the exception and all the previous exceptions in the chain). So the exception is not really uncaught.

weirdan
  • 2,499
  • 23
  • 27
  • OK, thanks for the explanation. So if I understand you right I need to config my PHP setup that it won't exit on a Uncaught Exception, but using this code in my onBootstrap() it still exits when there is an uncaught exception. – Fabb Aug 26 '18 at 21:32
  • «*So if I understand you right I need to config my PHP setup that it won't exit on a Uncaught Exception*» — Unless you're using something like php-pm or ReactPHP you don't need any specific configuration. «*but using this code in my onBootstrap() it still exits when there is an uncaught exception*» — There's not enough information to conclude that. Most likely there's some standard handler in Zend\Mvc that shows some error message in this case. – weirdan Aug 26 '18 at 23:29
  • I only have it enabled in the view, but even if the view does not display it it exits. So I'm curious how the code example can or could catch it all. – Fabb Aug 27 '18 at 03:33