Notifying Admins when unexpected error occurs

I am trying to build a process that can be used whenever an unexpected error (i.e. database off line, server issues, etc…)

What I would like to do is send a notification to designated people alerting them to the problem and include as much diagnostic or debugging information as possible; and display a message to the end user which apologizes for the inconvenience and letting them know that staff has been notified about this issue.

How can I capture and report the error information in the email?

Currently I have a php file that I bring in via an include (see below) to generate the email message and display the apology message.

<?PHP  
	unexpected_sys_error(__NAMESPACE__ . ' \ '. __FUNCTION__ . '\\' . __CLASS__, __METHOD__, "error msg holder", __FILE__, __LINE__, debug_print_backtrace());

	function unexpected_sys_error($pg_module, $pg_process, $err_message, $err_file, $err_line, $err_backtrace){
		
		$err_msg = 'pg_module: ' . $pg_module . PHP_EOL; 
		$err_msg .= 'pg_process: ' . $pg_process . PHP_EOL;
		$err_msg .= 'err_message: ' . $err_message . PHP_EOL;
		$err_msg .= 'err_file: ' . $err_file . PHP_EOL;
		$err_msg .= 'err_line: ' . $err_line . PHP_EOL;
		$err_msg .= 'err_backtrace: ' . $err_backtrace . PHP_EOL;
		
		
		mail("[email protected]","DEMO ALERT EMAIL",$err_msg);
	}
?>

I am not getting information about the error or the page that is calling the error. When I get the email, I was getting information for the system.unavailable.email.php page not where the issue was occurring.

I am not sure what I am doing wrong. Additionally, I would like to include additional information if possible to help diagnose the error.

I would recommend using Exceptions like

<?php // scratch_47.php
class FooException extends Exception {
    public function __construct($message = null, $code = 0, Exception $previous = null){
        print_r(func_get_args());
        print_r(debug_backtrace());
    }
}
<?php // scratch_48.php
require('scratch_47.php');
throw new FooException();

then your debug will show data from the file the exception occurs like

Array
(
    [0] => Array
        (
            [file] => scratch_48.php
            [line] => 3
            [function] => __construct
            [class] => FooException
            [object] => FooException Object
                (
                    [message:protected] => 
                    [string:Exception:private] => 
                    [code:protected] => 0
                    [file:protected] => scratch_48.php
                    [line:protected] => 3
                    [trace:Exception:private] => Array
                        (
                        )

                    [previous:Exception:private] => 
                )

When i call this exception from other pages, will I be getting the information about what was going on when the error occurred or will it show the __construct function as above?

The cleanest solution is to use the built-in set_exception_handler function.
https://www.php.net/manual/en/function.set-exception-handler.php

Here is a sample usage.

function custom_exception($exception)
{
    echo '<div class="error_custom col-md-12"><b>Fatal Error!</b>';
    $error_msg = "DATE: " . MYSQL_DATETIME_TODAY . "\nERROR: " . $exception->getMessage() . "\nFILE: " . $exception->getFile() . ' on line ' . $exception->getLine() . "\n\nSTACK TRACE\n" . $exception->getTraceAsString() . "\n";
    if (EMAIL_ERROR)
    {
        echo '<br>Admin has been notified';
        error_log("$error_msg", 1, ADMIN_EMAIL_TO, "From:" . ADMIN_EMAIL_FROM);
    }
    else
    {
        echo '<br>Admin has not been notified';
    }
    // Write error to log
    if (LOG_ERROR)
    {
        echo '<br>Error has been logged';
        error_log("$error_msg\r\n", 3, ERROR_LOG_PATH);
    }
    else
    {
        echo '<br>Error has not been logged';
    }
    echo '</div>';
    if (DEBUG)
    {
        echo '<div class="error_custom col-md-12"><b>Error Message:</b>';
        echo '<pre>';
        echo $exception->getMessage();
        echo '<br>FILE: ' . $exception->getFile();
        echo '<br>on line ' . $exception->getLine();
        echo '</pre>';
        echo '</div>';
        echo '<div class="error_custom"><b>Stack Trace:</b><br>';
        echo '<pre>';
        echo $exception->getTraceAsString();
        echo '</pre>';
        echo '</div>';
    }
}
Sponsor our Newsletter | Privacy Policy | Terms of Service