I want to add error handling in module.php
to add all error messages in flash messenger and redirect to an specific page (in my requirement) :
public function handleError(MvcEvent $e) {
$exception = $e->getParam('exception');
$controller = $e->getTarget();
//echo $exception->getMessage(); exit;
if (!$e->getApplication()->getServiceManager()->get('AuthService')->hasIdentity()) {
$controller->flashMessenger()->addErrorMessage("Session Expired..!!");
return $e->getTarget()->plugin('redirect')->toRoute('auth', array('action' => 'login'));
}
switch ($exception->getCode()) {
case "2003" :
$controller->flashMessenger()->addErrorMessage("Unable to connect database..!!");
break;
default :
$controller->flashMessenger()->addErrorMessage($exception->getMessage());
break;
}
$e->getApplication()->getServiceManager()->get('AuthService')->clearIdentity();
return $e->getTarget()->plugin('redirect')->toRoute('auth', array('action' => 'login'));
}
But in some errors its throwing call to undefined method plugin on $e->getTarget()
because in some cases error are generating before plugin bindings. I want a way to access redirect and flash messenger plugins
without referring any controller.