Quantcast

Dealing with uncatched exceptions and using set_exception_handler() in Zend Framework

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Dealing with uncatched exceptions and using set_exception_handler() in Zend Framework

electrotype
This post has NOT been accepted by the mailing list yet.
In the previous framework I used, there was a global exception handler defined in the bootstrap (using set_exception_handler()). This way, if an exception occured and was not catched, if was still possible to output something "nice", not just a blank page.

Is there an already existing way of dealing with uncatched exception in ZF?

Also, if I use my own exception handler (using set_exception_handler()), I see that there is an instance of set_exception_handler() in the constructor of Zend_Rest_Server! This is bad because it would overwrite my own exception handler if I use one! I think Zend_Rest_Server should at least use restore_exception_handler() in its destructor to restore the previous handler, if one existed.

How does ZF suggest to deal with uncatched exceptions?

Thanks in advance!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Dealing with uncatched exceptions and using set_exception_handler() in Zend Framework

dmitrybelyakov
Not sure about the Rest_Server, but generally there are two types of exception you can get:

1. Critical exceptions, that happen during application boostrap process
2. MVC Exceptions that happen during dispatch process and afterwards

If you want to handle exceptions of first type you may wrap actual execution of your aplication in a try-catch in your index.php file and redirect them to the appropriate handler like so:

try
{
    $application = new Zend_Application(
        APPLICATION_ENVIRONMENT,
        APPLICATION_PATH . '/configs/application.ini');
    $application->bootstrap();
    $application->run();
}
catch(Exception $exception)
{
    $YourExceptionHandler::handleException($exception);
}

This will catch all critical exceptions that may break application execution process. All other exceptions (MVC exceptions) can be cought at the dispatch process. For example you may use a front controller plugin that catches MVC exceptions and handles them accordingly.

If you wish you can even utilize a single handler class to handle both types of exceptions and perform certain actions, like for example logging exception data with Zend_Log, sending emails or just printing out.


Hope that helps, good luck,
Dmitry.



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Dealing with uncatched exceptions and using set_exception_handler() in Zend Framework

electrotype
Thanks dmitrybelyakov. I'll probably use this try/catch in the index file...

I still think Zend_Rest_Server should be changed tough.
Loading...