Reputation: 1251
I have a project which is developed in Zend Framework 1. The project is completed.
Now while I am testing the whole site, some pages thrown exceptions. One of such is below:
exception 'Zend_Paginator_Exception' with message 'No adapter for type NULL'
I have searched in net and got the steps to add try-catch to this. But it will take much time to check all code and repeat this step.
Can I write a common exception handler which catches all exceptions and handle it ?
Upvotes: 0
Views: 1924
Reputation: 2186
If you have this line in your index.php
$application->bootstrap()->run();
You can wrap it with try
catch
block
try {
$application->bootstrap()->run();
} catch (Exception $e) {
//handle all exceptions here
}
Of course you can also have many catch blocks for different types of exceptions.
Upvotes: 0
Reputation: 5651
Zend framework automatically handles exceptions using the errorController. You can enable it by adding the following line in you config file.
resources.frontController.throwExceptions = 0
If you want to handle exceptions manually then, rather then writing try/catch block all over the code you can centralize it using the code below.
Tell Zend Framework to not handle exceptions. Do this in your application.ini
resources.frontController.throwExceptions = 1
Define a custom method to handle exceptions in your Bootstrap class.
public function __handleExceptions(Exception $e){
//render a view with a simple error message for the user
//and send an email with the error to admin
}
Override the _bootstrap()
and run()
methods of Zend_Application_Bootstrap_Bootstrap in your Bootstrap class as shown below. This will catch all the exceptions thrown during the bootstrapping and request dispatching process and call your custom exception handler.
protected function _bootstrap($resource = null)
{
try {
parent::_bootstrap($resource);
} catch(Exception $e) {
$this->__handleExecptions($e);
}
}
public function run()
{
try {
parent::run();
} catch(Exception $e) {
$this->__handleExecptions($e);
}
}
This will eliminate the need of writing try/catch block at multiple places. Hope this helps.
Upvotes: 1