php error handler
v2.0.2
翻译:西班牙语
用于处理异常的 PHP 库。
要处理异常,您可以使用异常处理程序库。
操作系统:Linux |视窗。
PHP 版本:8.1。
安装此扩展的首选方法是通过 Composer。
要安装PHP ErrorHandler 库,只需:
composer require josantonius/error-handler
前面的命令只会安装必要的文件,如果您想下载完整的源代码,您可以使用:
composer require josantonius/error-handler --prefer-source
您还可以使用 Git克隆完整的存储库:
git clone https://github.com/josantonius/php-error-handler.git
JosantoniusErrorHandlerErrorException
扩展了 ErrorException
获取错误文件:
public function getFile(): string ;
获取错误级别:
public function getLevel(): int ;
获取错误文件行:
public function getLine(): int ;
获取错误信息:
public function getMessage(): string ;
获取错误名称:
public function getName(): string ;
JosantoniusErrorHandlerErrorHandled
获取错误文件:
public function getFile(): string ;
获取错误级别:
public function getLevel(): int ;
获取错误文件行:
public function getLine(): int ;
获取错误信息:
public function getMessage(): string ;
获取错误名称:
public function getName(): string ;
JosantoniusErrorHandlerErrorHandler
将错误转换为异常:
/**
* The errors will be thrown from the ErrorException instance.
*
* @param int[] $errorLevel Define the specific error levels that will become exceptions.
*
* @throws WrongErrorLevelException if error level is not valid.
*
* @see https://www.php.net/manual/en/errorfunc.constants.php to view available error levels.
*/
public function convertToExceptions( int ... $ errorLevel ): ErrorHandler ;
将错误转换为异常,但其中一些错误除外:
/**
* The errors will be thrown from the ErrorException instance.
*
* @param int[] $errorLevel Define the specific error levels that will become exceptions.
*
* @throws WrongErrorLevelException if error level is not valid.
*
* @see https://www.php.net/manual/en/errorfunc.constants.php to view available error levels.
*/
public function convertToExceptionsExcept( int ... $ errorLevel ): ErrorHandler ;
注册错误处理函数:
/**
* The error handler will receive the ErrorHandled object.
*
* @see https://www.php.net/manual/en/functions.first_class_callable_syntax.php
*/
public function register( callable $ callback ): ErrorHandler ;
使用错误报告来确定处理哪些错误:
/**
* If the setting value in error_reporting() is used to determine which errors are handled.
*
* If this method is not used, all errors will be sent to the handler.
*
* @see https://www.php.net/manual/en/function.error-reporting.php
*/
public function useErrorReportingLevel(): ErrorHandler ;
use Josantonius ErrorHandler Exceptions WrongErrorLevelException ;
该库的使用示例:
use Josantonius ErrorHandler ErrorHandler ;
$ errorHandler = new ErrorHandler ();
$ errorHandler -> convertToExceptions ();
// All errors will be converted to exceptions.
use Josantonius ErrorHandler ErrorHandler ;
$ errorHandler = new ErrorHandler ();
$ errorHandler -> convertToExceptions ( E_USER_ERROR , E_USER_WARNING );
// Only E_USER_ERROR and E_USER_WARNING will be converted to exceptions.
use Josantonius ErrorHandler ErrorHandler ;
$ errorHandler = new ErrorHandler ();
$ errorHandler -> convertToExceptionsExcept ( E_USER_DEPRECATED , E_USER_NOTICE );
// All errors except E_USER_DEPRECATED and E_USER_NOTICE will be converted to exceptions.
use Josantonius ErrorHandler ErrorHandler ;
error_reporting ( E_USER_ERROR );
$ errorHandler = new ErrorHandler ();
$ errorHandler -> convertToExceptions ()-> useErrorReportingLevel ();
// Only E_USER_ERROR will be converted to exception.
use ErrorException ;
use Josantonius ErrorHandler ErrorHandler ;
set_exception_handler ( function ( ErrorException $ exception ) {
var_dump ([
' level ' => $ exception -> getLevel (),
' message ' => $ exception -> getMessage (),
' file ' => $ exception -> getFile (),
' line ' => $ exception -> getLine (),
' name ' => $ exception -> getName (),
]);
});
$ errorHandler = new ErrorHandler ();
$ errorHandler -> convertToExceptions ();
// All errors will be converted to exceptions.
use Josantonius ErrorHandler ErrorHandled ;
use Josantonius ErrorHandler ErrorHandler ;
function handler ( Errorhandled $ errorHandled ): void {
var_dump ([
' level ' => $ errorHandled -> getLevel (),
' message ' => $ errorHandled -> getMessage (),
' file ' => $ errorHandled -> getFile (),
' line ' => $ errorHandled -> getLine (),
' name ' => $ errorHandled -> getName (),
]);
}
$ errorHandler = new ErrorHandler ();
$ errorHandler -> register (
callback: handler (...)
);
// All errors will be converted to exceptions.
use Josantonius ErrorHandler ErrorHandled ;
use Josantonius ErrorHandler ErrorHandler ;
class Handler {
public static function errors ( Errorhandled $ exception ): void { /* do something */ }
}
$ errorHandler = new ErrorHandler ();
$ errorHandler -> register (
callback: Handler:: errors (...)
)-> convertToExceptions ();
// The error will be sent to the error handler and then throw the exception.
error_reporting ( E_USER_ERROR );
class Handler {
public function errors ( Errorhandled $ exception ): void { /* do something */ }
}
$ handler = new Handler ();
$ errorHandled -> register (
callback: $ handler -> errors (...),
)-> convertToExceptions ()-> useErrorReportingLevel ();
// Only E_USER_ERROR will be passed to the handler and converted to exception.
要运行测试,您只需要 Composer 并执行以下命令:
git clone https://github.com/josantonius/php-error-handler.git
cd php-error-handler
composer install
使用 PHPUnit 运行单元测试:
composer phpunit
使用 PHPCS 运行代码标准测试:
composer phpcs
运行 PHP Mess Detector 测试来检测代码风格中的不一致:
composer phpmd
运行之前的所有测试:
composer tests
每个版本的详细更改都记录在发行说明中。
在发出拉取请求、开始讨论或报告问题之前,请务必阅读贡献指南。
感谢所有贡献者! ❤️
如果这个项目可以帮助您减少开发时间,您可以资助我支持我的开源工作吗?
该存储库已根据 MIT 许可证获得许可。
版权所有 © 2016 年至今,Josantonius