Zend framework 2 recommended way for error handling

15,903

You can handle the exceptions in anyway you want after catching it as the following example in which you are catching the exception globally...:

In the onBootstrap method in your Module.php you can attach a function to execute when an event occurs, the following attach a function to be executed when an error (exception) is raised:

public function onBootstrap(MvcEvent $e)
{
    $application = $e->getApplication();
    $em = $application->getEventManager();
    //handle the dispatch error (exception) 
    $em->attach(\Zend\Mvc\MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'handleError'));
    //handle the view render error (exception) 
    $em->attach(\Zend\Mvc\MvcEvent::EVENT_RENDER_ERROR, array($this, 'handleError'));
}

and then define the function to handle the error in any way you want, the following is an example:

public function handleError(MvcEvent $e)
{
    //get the exception
    $exception = $e->getParam('exception');
    //...handle the exception... maybe log it and redirect to another page, 
    //or send an email that an exception occurred...
}
Share:
15,903
Teodor Talov
Author by

Teodor Talov

A smart accountant once told said that the answer to “How much money did you make?” is always, “Who wants to know?” If it’s an investor, the answer is “A lot.” If it’s a customer, the answer is “A little.” If it’s the IRS, the answer is “None.” Same thing with programmers. The answer to “Who is a good programmer?” is always, “Who wants to know?” To a project manager, the programmer who hits every deadline (regardless of quality) is a good programmer. To a customer, the programmer who solves their problem quickest is a good programmer. To a business owner, the programmer who makes them the most money is a good programmer. To a PHB, the programmer who makes them look the best is a good programmer. To a junior programmer, the best mentor is the good programmer. To another programmer, the programmer they are most likely to want to go into battle with is a good programmer. Ed Weissman

Updated on June 19, 2022

Comments

  • Teodor Talov
    Teodor Talov almost 2 years

    I noticed that the Skeleton Application that Zend provides does not handle error 500. I know that in ZF1 there was an ErrorController that took care of that. I have done some research online, but did not find a clear cut solution for this.

    So what is the best way for error handling in ZF2. Would it be on per module basis or some global exception/error handler?

    I know that another solution would be to add ini_set('display_errors', true); to my index.php, but I don't really like that solution. It seems that the framework should provide some way for handling errors.

  • srayner
    srayner over 8 years
    Can you show us how we redirect to another page from within handleError function?
  • Khaled AbuShqear
    Khaled AbuShqear over 5 years
    we're running APIs application, working like a charm, thanks man :)
  • Akhil VL
    Akhil VL over 4 years
    @Mohammad Control doesnt come to handlerError() if in header we send: application/json. Any idea how to fix this? -H 'Accept: application/json'