I'm trying to customise the exception error with my custom error message as per below:
try {
$test = 1/0;
}
catch(Exception $ex){
$t=time();
$timelog = date("dmYhms",$t);
error_log($timelog."=> ErrorID: ".$timelog."\n", 3, "../error_log/Error.log");
echo "System having issue with this request.\n
Transaction ID: ".$timelog;
}
My expectation was, I managed to echo
the custom message instead the default message and I managed to write in the log file. But, I'm still getting the default error message instead the custom one as per image below.
Any idea where I did the mistake. Please advice.
EDITED
error_reporting(0);
//error handler function
function customError($errno, $errstr) {
//echo "<b>Error:</b> [$errno] $errstr<br>";
$t=time();
$timelog = date("dmYhms",$t);
echo "Unable to process with the request. \n Error ID".$timelog.". PLease contact RND team.";
error_log($timelog."=> Error: [$errno] $errstr\n", 3, "error_log/Error.log");
}
//set error handler
set_error_handler("customError",E_USER_WARNING);
//trigger error
$username = "xxx";
$password = "xxx";
$host = "xxxx";
$dbname = "xxx";
$db = new mysqli($host, $username, $password,$dbname);
if ($db->connect_error) {
trigger_error($db->connect_error,E_USER_WARNING);
}
The above edited sample working fine but, is this the best practice. Please advice.