Last active
January 22, 2023 02:45
-
-
Save timmyRS/1ae9616a76b0c1b07a88288d8cfafbdf to your computer and use it in GitHub Desktop.
The perfect PHP error handler.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// This changes the error handler to a more helping one. | |
// You must/can prepend this script - Help: http://stackoverflow.com/a/35298767/4796321 | |
function h_error($severity, $message, $file, $line) | |
{ | |
if (!(error_reporting() & $severity)) | |
{ | |
return; | |
} | |
throw new ErrorException($message, 0, $severity, $file, $line); | |
} | |
function h_exception(Exception $e) | |
{ | |
$msg = $e->getMessage(); | |
echo "<strong>{$msg}</strong> (".get_class($e).") on <strong>{$e->getFile()}</strong> ({$e->getLine()}) ["; | |
$msg = urlencode($msg); | |
echo "<a target='_blank' href='http://stackoverflow.com/search?q=%5Bphp%5D+{$msg}'>Search</a> or "; | |
echo "<a target='_blank' href='http://stackoverflow.com/questions/ask?title=PHP:%20{$msg}&tags=PHP'>Ask</a> on "; | |
echo "Stackoverflow]<br>"; | |
} | |
set_error_handler("h_error"); | |
set_exception_handler("h_exception"); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment