五实验室 / error
该包已被废弃且不再维护。未建议替代包。
控制多个系统中的错误代码
v1.0
2015-09-15 13:51 UTC
Requires
- php: >=5.4
Requires (Dev)
- phpunit/phpunit: 4.*
This package is not auto-updated.
Last update: 2017-08-16 12:51:51 UTC
README
使用此包,您可以控制具有许多子系统的系统中的错误代码和异常。
安装
在您的 composer.json 中添加 FiveLab/Error
{ "require": { "fivelab/error": "~1.0" } }
现在运行以下命令让 composer 下载库
$ php composer.phar update fivelab/error
基本用法
为了收集所有错误代码,您必须在您的子系统创建一个错误工厂
use FiveLab\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 FiveLab\Component\Error\Errors; $errors = new Errors(); $errors->addFactory(new MySystemError());
之后,您可以
- 为每个系统获取保留的代码段
$errors->getReservedCodes();
- 获取系统中的所有错误
$errors->getErrors();
- 获取所有异常
$exceptions = $errors->getExceptions();
- 检查错误存储中是否有异常
$exception = new \Exception(); $errors->hasException($exception);
- 获取异常的错误代码
$exception = new \Exception(); $code = $errors->getExceptionCode($exception);
- 检查保留的代码
$errors->checkReservedCodes();