控制多个系统的错误代码

v1.0 2015-06-04 08:59 UTC

This package is not auto-updated.

Last update: 2024-09-28 17:17:52 UTC


README

使用本包,您可以在具有多个子系统的系统中控制错误代码和异常。

安装

在您的 composer.json 中添加 FivePercent/Error

{
    "require": {
        "fivepercent/error": "~1.0"
    }
}

现在运行命令来告诉 composer 下载库

$ php composer.phar update fivepercent/error

基本用法

为了收集所有错误代码,您必须在您的子系统创建一个错误工厂

use FivePercent\Component\Error\ErrorFactoryInterface;

final class MySystemError implements ErrorFactoryInterface
{
    const ERROR_1 = 1;
    const ERROR_2 = 2;

    public function getErrors()
    {
        return [
            self::ERROR_1 => 'Error #1',
            self::ERROR_2 => 'Error #2'
        ];
    }

    public function getExceptions()
    {
        return [];
    }

    public function getReservedDiapason()
    {
        return [1, 5];
    }
}

并创建错误系统(存储),然后将此工厂添加到存储中

use FivePercent\Component\Error\Errors;

$errors = new Errors();
$errors->addFactory(new MySystemError());

之后,您可以

  1. 获取每个系统的保留范围
    $errors->getReservedCodes();
  2. 获取系统的所有错误
    $errors->getErrors();
  3. 获取所有异常
    $exceptions = $errors->getExceptions();
  4. 检查错误存储中是否有异常
    $exception = new \Exception();
    $errors->hasException($exception);
  5. 获取异常的错误代码
    $exception = new \Exception();
    $code = $errors->getExceptionCode($exception);
  6. 检查保留的代码
    $errors->checkReservedCodes();