calgamolib/exception

此包已被 弃用 且不再维护。作者建议使用 knot-lib/exception 包。

异常基类库

0.2.0 2019-12-03 23:01 UTC

This package is auto-updated.

Last update: 2019-12-09 09:17:27 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Code Climate Total Downloads

描述

异常基类库

类层次结构

\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';

许可

MIT

作者

stk2k

免责声明

本软件不提供任何保证。

我们不承担因使用本软件而产生的任何后果。

请自行承担责任。