Skip to content

Instantly share code, notes, and snippets.

@liamkeily
Created April 25, 2019 11:19
Show Gist options
  • Select an option

  • Save liamkeily/ea6c776f2b75d206323490b8748254cd to your computer and use it in GitHub Desktop.

Select an option

Save liamkeily/ea6c776f2b75d206323490b8748254cd to your computer and use it in GitHub Desktop.
Convert PHP user_error to Exceptions using set_error_handler
set_error_handler(function(int $severity, string $message, string $file, int $line) {
if (error_reporting()) {
throw new ErrorException($message, $severity, $severity, $file, $line);
}
});
try {
user_error('Something bad happened');
}
catch (ErrorException $e)
{
echo 'This does get caught: ' . $e->getMessage() . PHP_EOL;
}
try {
@user_error('Something bad happened');
}
catch (ErrorException $e)
{
echo 'This does not get caught: ' . $e->getMessage() . PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment