0

In Zend 2 & zend 3 I've experiencing this issue. The following code generates "YourModule\Controller\ClassName" not found since i'm not imported the class ClassName.

<?php
public function indexAction() {
    $x = new ClassName() ; //I've not imported ClassName, which raise the  error.
}

When these type of some exception occurs the control goes to a catch section in onDistpach (DispatchListner). If i echo $ex->getMessage() in this catch block it prints proper error message eg: "YourModule\Controller\ClassName" not found.

public function onDispatch(MvcEvent $e)
{
    ....

} catch (\Throwable $ex) {
            $caughtException = $ex; //HERE
        //$ex->getMessage() ;
}

But the final output rendered by zend is following. There is no information about the exception occurred.

An error occurred An error occurred during execution;

please try again later.

No Exception available

Most other cases it print proper stacktrace. How can I configure zend to display these error messages without editing DispatchListner in Zend-Mvc ?

Edit: I tried turning on error_reporting() & display_errors just before the exception. Also tried try catch around the code which generate exception and still not works. Also my module.config.

'view_manager' => array(
    'display_not_found_reason' => true,
    'display_exceptions' => true,
...
Community
  • 1
  • 1
nithinTa
  • 1,632
  • 2
  • 16
  • 32
  • Possible duplicate of [Display php errors when using Zend framework](https://stackoverflow.com/questions/1201709/display-php-errors-when-using-zend-framework) – Huso Oct 27 '17 at 07:35
  • Or https://stackoverflow.com/questions/6392265/how-to-check-full-error-log-in-zend-framework – Huso Oct 27 '17 at 07:36
  • Or https://stackoverflow.com/questions/3941860/zend-framework-not-all-errors-are-shown – Huso Oct 27 '17 at 07:37
  • @Boratzan are you sure ? – nithinTa Oct 27 '17 at 07:40
  • Yes, I'm sure if you check those answers, it will solve your problem.. – Huso Oct 27 '17 at 07:41
  • All 3 belong to zf1 and is there any solution in zf2 & zf3 ? error_reporting(E_ALL|E_STRICT); ini_set('display_errors', 'on'); is already on. I'm receiving error informations about most errors, but some exception are only shows as above. – nithinTa Oct 27 '17 at 07:48
  • Since you can't find it, I'll just post the answer :) – Huso Oct 27 '17 at 07:51
  • Have a look at the Skeleton Application file `error\index.phtml`, line #6, `if()` statement. The last line "No Exception available" comes from the `else{}`. You could instead dump or print the whole exception and/or message there if you want. As a better option I would suggest you make sure you use an editor, such as [PhpStorm](https://www.jetbrains.com/phpstorm/), that supports Xdebug and debug your code ;) [PHP xdebug extension](https://xdebug.org/wizard.php), [Chrome extension (also for Vivaldi)](https://chrome.google.com/webstore/detail/xdebug-helper/eadndfjplgieldjbigjakmdgkmoaaaoc?hl=en) – rkeet Oct 27 '17 at 14:48

2 Answers2

6

Exceptions in dispatch and render can be tracked by attaching a listener to MvcEvent::EVENT_RENDER_ERROR and MvcEvent::EVENT_DISPATCH_ERROR. This is ZF3 solution might work in ZF2 also.

My Own error render function with a red title.

public function exception($e) {
    echo "<span style='font-family: courier new; padding: 2px 5px; background:red; color: white;'> " . $e->getMessage() . '</span><br/>' ;
    echo "<pre>" . $e->getTraceAsString() . '</pre>' ;   
}

Attach listeners on bootstrap

public function onBootstrap(MvcEvent $e)
{
    $eventManager        = $e->getApplication()->getEventManager();
    $moduleRouteListener = new ModuleRouteListener();
    $moduleRouteListener->attach($eventManager);

    //Attach render errors
    $eventManager->attach(MvcEvent::EVENT_RENDER_ERROR, function($e)  {
        if ($e->getParam('exception')) {
            $this->exception( $e->getParam('exception') ) ; //Custom error render function.
        }
    } );
    //Attach dispatch errors
    $eventManager->attach(MvcEvent::EVENT_DISPATCH_ERROR, function($e)  {
        if ($e->getParam('exception')) {
            $this->exception( $e->getParam('exception') ) ;//Custom error render function.
        }
    } );
}
nithinTa
  • 1,632
  • 2
  • 16
  • 32
  • 1
    Your solution works. I just can't understand why ZF has wiped out these kind of errors – leoap May 29 '18 at 11:47
1

Set display_exceptions to true in your module.config.php file:

<?php
 'view_manager' => array(    
 'display_not_found_reason' => true,
 'display_exceptions'       => true, // SET TO true

Don't forget to set it back to false when your working in a production environment.

Huso
  • 1,501
  • 9
  • 14