calgamolib / exception
0.2.0
2019-12-03 23:01 UTC
Requires
- php: >=7.1
- ext-json: *
- stk2k/collection: ~0.1
Requires (Dev)
- php-coveralls/php-coveralls: ^2.0
- phpunit/phpunit: ^6.3.0
This package is auto-updated.
Last update: 2019-12-09 09:17:27 UTC
README
描述
异常基类库
类层次结构
\Exception
|
+-- CalgamoException
|
+--[Package]Logic
| |
| +-- PhpErrorException
|
+--[Package]Runtime
|
+-- ClassNotFoundException
|
+-- HttpStatusException
|
+-- InterfaceNotFoundException
|
+-- InvalidArgumentException
|
+-- PhpParseException
注意
- [必须]你绝对不能捕获逻辑异常
- [必须]你必须捕获运行时异常
接口层次结构
\Throwable
|
+-- CalgamoExceptionInterface
|
+--[Package]Logic
| |
| +-- LogicExceptionInterface
|
+--[Package]Runtime
|
+-- BusinessExceptionInterface
|
+-- ConfigExceptionInterface
|
+-- RuntimeExceptionInterface
注意
- 你可以通过接口捕获异常,而不是通过具体的异常类名称捕获。
try{ doSomething(); } catch(BusinessExceptionInterface $e) { // This typically means recoverable error, so you can process the exception and continue the procedure. } catch(RuntimeExceptionInterface $e) { // This typically means fatal error, so you should report the situation(my be logs) and abort the procedure. }
回溯
每个 CalgamoException 对象都有自己的回溯。因此,你可以通过调用 CalgamoException#getBackTraceList() 方法来跟踪调用历史。
try{ doSomething(); } catch(CalgamoException $e) { foreach($e->getBackTraceList() as $trace){ // $trace is always instance of BacktraceItem $file = $trace->getFile(); $line = $trace->getLine(); $class = $trace->getClass(); $type = $trace->getType(); $function = $trace->getFunction(); echo "{$class}{$type}{$function} called at {$file}/{$line}" . PHP_EOL; }); }
需求
PHP 7.1 或更高版本
安装 calgamolib/exception
安装 calgamolib/exception 的推荐方式是通过 Composer。
composer require calgamolib/exception
安装后,你需要要求 Composer 的自动加载器
require 'vendor/autoload.php';
许可
作者
免责声明
本软件不提供任何保证。
我们不承担因使用本软件而产生的任何后果。
请自行承担责任。