Created
April 25, 2019 11:19
-
-
Save liamkeily/ea6c776f2b75d206323490b8748254cd to your computer and use it in GitHub Desktop.
Convert PHP user_error to Exceptions using set_error_handler
This file contains hidden or 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
| 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