Created
January 6, 2017 09:54
-
-
Save starx/aa4471b4b569aad0a5d0ca2f7d824bd4 to your computer and use it in GitHub Desktop.
PHP Catching Error as Exception (Credits: http://php.net/manual/en/function.set-error-handler.php#112881)
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
<?php | |
/** | |
* throw exceptions based on E_* error types | |
*/ | |
set_error_handler(function ($err_severity, $err_msg, $err_file, $err_line, array $err_context) | |
{ | |
// error was suppressed with the @-operator | |
if (0 === error_reporting()) { return false;} | |
switch($err_severity) | |
{ | |
case E_ERROR: throw new ErrorException ($err_msg, 0, $err_severity, $err_file, $err_line); | |
case E_WARNING: throw new WarningException ($err_msg, 0, $err_severity, $err_file, $err_line); | |
case E_PARSE: throw new ParseException ($err_msg, 0, $err_severity, $err_file, $err_line); | |
case E_NOTICE: throw new NoticeException ($err_msg, 0, $err_severity, $err_file, $err_line); | |
case E_CORE_ERROR: throw new CoreErrorException ($err_msg, 0, $err_severity, $err_file, $err_line); | |
case E_CORE_WARNING: throw new CoreWarningException ($err_msg, 0, $err_severity, $err_file, $err_line); | |
case E_COMPILE_ERROR: throw new CompileErrorException ($err_msg, 0, $err_severity, $err_file, $err_line); | |
case E_COMPILE_WARNING: throw new CoreWarningException ($err_msg, 0, $err_severity, $err_file, $err_line); | |
case E_USER_ERROR: throw new UserErrorException ($err_msg, 0, $err_severity, $err_file, $err_line); | |
case E_USER_WARNING: throw new UserWarningException ($err_msg, 0, $err_severity, $err_file, $err_line); | |
case E_USER_NOTICE: throw new UserNoticeException ($err_msg, 0, $err_severity, $err_file, $err_line); | |
case E_STRICT: throw new StrictException ($err_msg, 0, $err_severity, $err_file, $err_line); | |
case E_RECOVERABLE_ERROR: throw new RecoverableErrorException ($err_msg, 0, $err_severity, $err_file, $err_line); | |
case E_DEPRECATED: throw new DeprecatedException ($err_msg, 0, $err_severity, $err_file, $err_line); | |
case E_USER_DEPRECATED: throw new UserDeprecatedException ($err_msg, 0, $err_severity, $err_file, $err_line); | |
} | |
}); | |
class WarningException extends ErrorException {} | |
class ParseException extends ErrorException {} | |
class NoticeException extends ErrorException {} | |
class CoreErrorException extends ErrorException {} | |
class CoreWarningException extends ErrorException {} | |
class CompileErrorException extends ErrorException {} | |
class CompileWarningException extends ErrorException {} | |
class UserErrorException extends ErrorException {} | |
class UserWarningException extends ErrorException {} | |
class UserNoticeException extends ErrorException {} | |
class StrictException extends ErrorException {} | |
class RecoverableErrorException extends ErrorException {} | |
class DeprecatedException extends ErrorException {} | |
class UserDeprecatedException extends ErrorException {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment