Skip to content

Instantly share code, notes, and snippets.

@timw4mail
Created December 18, 2013 21:22
Show Gist options
  • Save timw4mail/8030083 to your computer and use it in GitHub Desktop.
Save timw4mail/8030083 to your computer and use it in GitHub Desktop.
Catching errors
<?php
/**
* Function to run on script shutdown
* -used to catch most fatal errors, and
* display them cleanly
*
* @return void
*/
function shutdown()
{
// Catch the last error
$error = error_get_last();
// types of errors that are fatal
$fatal = array(E_ERROR, E_PARSE, E_RECOVERABLE_ERROR);
// Display pretty error page
if (in_array($error['type'], $fatal))
{
$file = $error['file']);
$err_msg = "<h2>Fatal Error: </h2>
{$error['message']}<br /><br />
<strong>File:</strong> {$file}<br /><br />
<strong>Line:</strong> {$error['line']}";
show_error($err_msg);
}
}
// Runs at execution shutdown
register_shutdown_function('shutdown');
/**
* Convert errors to exceptions
*
* @param int $severity
* @param string $message
* @param string $filepath
* @param int $line
* @return ErrorException
*/
function on_error($severity, $message, $filepath, $line)
{
throw new \ErrorException($message, 0, $severity, $filepath, $line);
}
// Runs on any error
set_error_handler('on_error');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment