josantonius/error-handler

用于处理错误的PHP库。

v2.0.2 2022-09-29 15:57 UTC

This package is auto-updated.

Last update: 2024-08-29 05:44:37 UTC


README

Latest Stable Version License Total Downloads CI CodeCov PSR1 PSR4 PSR12

翻译西班牙语

用于处理异常的PHP库。

要处理异常,您可以使用exception-handler库。

要求

  • 操作系统:Linux | Windows。

  • 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

可用类

ErrorException类

Josantonius\ErrorHandler\ErrorException 扩展 ErrorException

获取错误文件

public function getFile(): string;

获取错误级别

public function getLevel(): int;

获取错误文件行

public function getLine(): int;

获取错误消息

public function getMessage(): string;

获取错误名称

public function getName(): string;

ErrorHandled类

Josantonius\ErrorHandler\ErrorHandled

获取错误文件

public function getFile(): string;

获取错误级别

public function getLevel(): int;

获取错误文件行

public function getLine(): int;

获取错误消息

public function getMessage(): string;

获取错误名称

public function getName(): string;

ErrorHandler类

Josantonius\ErrorHandler\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://php.ac.cn/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://php.ac.cn/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://php.ac.cn/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://php.ac.cn/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

待办事项

  • 添加新功能
  • 改进测试
  • 改进文档
  • 改进README文件中的英文翻译
  • 重构代码以禁用代码风格规则(请参阅phpmd.xml和phpcs.xml)

变更日志

每个版本的详细更改记录在发布说明中。

贡献

请在提交拉取请求、开始讨论或报告问题之前,务必阅读贡献指南

感谢所有贡献者!❤️

赞助

如果这个项目帮助您减少了开发时间,您可以通过赞助我来支持我的开源工作 😊

许可证

本存储库采用MIT 许可证授权。

版权所有 © 2016-至今,Josantonius