Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am using zend framework 1.12 for my project. I want to catch all types of fatal errors and send them to an email address for quick fix. I have written the below mentioned code in Bootstrap.php file for this purpose.

protected function _initFatalErrorCatcher()
{
    register_shutdown_function(array($this, 'errorlogHandler'));
}

public function errorlogHandler()
{
    $e = error_get_last();

if (!is_null($e)) { //fatal error

    $msg  = 'Fatal error: ' . $e['message'];
    $msg .= ' in' . $e['file'];
    $msg .= ' on line: ' . $e['line'];

    $mail = new Zend_Mail('utf-8');
    $mail->setBodyHtml($msg);
    $mail->setFrom('[email protected]');
    $mail->addTo('[email protected]');
    $mail->setSubject('check this error');

    $mail->send();
    }
}

Using the above code, i am able to send fatal errors other than database connection related errors and query related errors to email. I followed the instructions from Catch Zend PDO Exception as well, but i believe i am missing something as its not working.

Any help on this will be appreciated.

EDIT:

I am also using Zend_Log to write the error logs in a log-file. But, using this i could not find a way to write the fatal errors. Code for this is given below.

$writer = new Zend_Log_Writer_Stream(APPLICATION_PATH  . "/../data/log-file.log");
$errors = $this->_getParam('error_handler');
$exception = $errors->exception;

$log = new Zend_Log($writer);
$log->debug($exception->getMessage() . "\n" . $exception->getTraceAsString());

Scenario for database connection related issue:

If there is any error in host name, database name or in user name, it shows a Fatal error in browser like below. But its not detected by register_shutdown_function() or Zend_Log().

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000] [1044] Access denied for user 'AAAA'@'%' to database 'BBBB'' in /var/www/project_name/library/Zend/Db/Adapter/Pdo/Abstract.php on line 144 PDOException: SQLSTATE[42000] [1044] Access denied for user 'AAAA'@'%' to database 'BBBB' in /var/www/project_name/library/Zend/Db/Adapter/Pdo/Abstract.php on line 129
share|improve this question
 
Look into Zend_Log –  Orangepill May 13 at 12:29
 
I already used that but using Zend_Log i didn't find a way to to list the fatal errors. Please check my edit part in the above post for details. –  Debashis May 13 at 12:38
 
Do you have an example of a type of error that this is failing on... really hard errors (like syntax errors) are not going to be resolvable by any means, at least that I'm aware of. –  Orangepill May 14 at 6:31
 
The place you want to put your try catch block is in the index.php, there is a line near the bottom that says $app->bootstrap()->run(); change that to $app->bootstrap(); try {$app->run()} catch(Exception $exception){ /*your exception log code*/ } –  Orangepill May 14 at 6:39
 
I tried your suggestion but not working. Please check 'Scenario for database connection related issue:' in above post. –  Debashis May 14 at 8:37
add comment

3 Answers

The post here shows an example. Basically use set_error_handler to tickle php into throwing exceptions when an error is encountered. This example from link:

<?php
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
    throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
set_error_handler("exception_error_handler");

/* Trigger exception */
strpos();
?>  

Hope this helps

share|improve this answer
 
The link is not working. Showing 'Page not found'. –  Debashis May 13 at 17:25
 
@debashis fixed the link –  Orangepill May 13 at 17:28
add comment

//$array contains the values for insert

try {
    $this->db->insert('Users', $array );
} catch (Exception $e){
    echo $e->getMessage();
}
share|improve this answer
 
Isn't there any other option to check this and database connection using index.php or bootstrap rather than implementing the try catch method in every single query? –  Debashis May 13 at 12:30
 
You Could include "/Zend/Db/Exception.php" in your index.php and wrap the whole Boostrapping with an try/catch (Zend_Db_Exception $e) –  ArneRie May 13 at 13:02
add comment
up vote 0 down vote accepted

I have solved it by writing the below mentioned code in Bootstrap.php file.

protected function _initDbConfig()
{
  $config = new Zend_Config($this->getOptions());
  $params = $config->database->toArray();
  try {
     $db = Zend_Db::factory('Pdo_Mysql', $params);
     $db->getConnection();

  } catch (Zend_Db_Adapter_Exception $e) {
    // perhaps the RDBMS is not running
    // code to send email goes here
  } catch (Zend_Exception $e) {
    // perhaps factory() failed to load the specified Adapter class
    // code to send email goes here         
  }
}

In application.ini, i have the following code.

database.host     = "localhost"
database.username = "AAAA"
database.password = "*****"
database.dbname   = "BBBBB"
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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