Welcome Guest, Not a member yet? Register   Sign In
Send Email and Update Database on Fatal Error in Specific Method
#1

[eluser]benners[/eluser]
I want to email errors and update the database if an error occurs during the exectution of a Method in a CI Controller.

The follow code is giving an error, "Message: set_error_handler() expects the argument (my_error_handler) to be a valid callback"

Code:
<?php
class Errors extends CI_Controller {


    public function __construct()
    {
        parent::__construct();

    }

    function my_error_handler($number, $message, $file, $line, $vars)
    {
        $email = "
            <p>An error ($number) occurred on line
            <strong>$line</strong> and in the <strong>file: $file.</strong>
            <p> $message </p>";

        $email .= "<pre>" . print_r($vars, 1) . "</pre>";

        $headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

        // Email the error to someone...
        error_log($email, 1, '[email protected]', $headers);

        // Update the database

        if ( ($number !== E_NOTICE) && ($number < 2048) ) {
            die("There was an error. Please try again later.");
        }
    }


    function test()
    {
        // Use custom function to handle errors.
        set_error_handler('my_error_handler');

        // Trigger an error... (var doesn't exist)
        echo $somevarthatdoesnotexist;

    }

}
?&gt;
#2

[eluser]PhilTem[/eluser]
I think this might help
Code:
function test()
    {
        // Use custom function to handle errors.
        set_error_handler(array(&$this, 'my_error_handler'));

        // Trigger an error... (var doesn't exist)
        echo $somevarthatdoesnotexist;

    }
since you have to tell the error-handler that it is a class-method you want to call (intended meaning of set_error_handler(array(&$this, 'method'))) and not just a function (intended meaning of set_error_handler('function'))
#3

[eluser]benners[/eluser]
Fantastic! Worked perfectly. Thank you.




Theme © iAndrew 2016 - Forum software by © MyBB