Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter catch database errors

I'm looking for a way to catch all the database errors (if and when they occur) and sent an error report my email. For regular php error I extended the CI_Exceptions class and added my email sending code in addition to logging. But the database errors don't go trough CI_Exceptions but instead are logged directly from CI_DB_Driver->query() and I don't want to modify any file in the System folder.

Also I don't want to write logging code around every query the app does.

like image 923
Dan F. Avatar asked Jul 12 '26 10:07

Dan F.


1 Answers

I'd be tempted to extend CI_Exceptions show_error method to catch any errors passed with the 'error_db' template rather than hunting through the db driver.

As you've already extended CI_Exceptions with email code this would seem like the best practice.

function show_error($heading, $message, $template = 'error_general', $status_code = 500)
{
    set_status_header($status_code);

    $message = '<p>'.implode('</p><p>', ( ! is_array($message)) ? array($message) : $message).'</p>';

    if ($template == 'error_db')
    {
        // do email send here with $message
    }

    if (ob_get_level() > $this->ob_level + 1)
    {
        ob_end_flush();
    }
    ob_start();
    include(APPPATH.'errors/'.$template.'.php');
    $buffer = ob_get_contents();
    ob_end_clean();
    return $buffer;
}
like image 175
Timmytjc Avatar answered Jul 18 '26 06:07

Timmytjc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!