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,
...
8

2 Answers 2

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.
        }
    } );
}
1
  • 1
    Your solution works. I just can't understand why ZF has wiped out these kind of errors
    – leoap
    May 29, 2018 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.

1
  • I have these settings set to true in module.config.php (ZF3) but am still not seeing an exception. Aug 16, 2018 at 10:06

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.