Skip to content

Instantly share code, notes, and snippets.

@timmyRS
Last active January 22, 2023 02:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timmyRS/1ae9616a76b0c1b07a88288d8cfafbdf to your computer and use it in GitHub Desktop.
Save timmyRS/1ae9616a76b0c1b07a88288d8cfafbdf to your computer and use it in GitHub Desktop.
The perfect PHP error handler.
<?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