knot-lib/exception

0.3.1 2021-06-05 19:52 UTC

This package is auto-updated.

Last update: 2023-09-06 00:42:04 UTC


README

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

描述

异常基类库

类层次结构

\Exception 
  |
  +-- KnotPhpException 
  |
  +--[Package]Logic
  |     |
  |     +-- PhpErrorException
  |
  +--[Package]Runtime
        |
        +-- ClassNotFoundException
        |
        +-- HttpStatusException
        |
        +-- InterfaceNotFoundException
        |
        +-- InvalidArgumentException
        |
        +-- PhpParseException

注意

  • [必须]您不得捕获逻辑异常
  • [必须]您必须捕获运行时异常

接口层次结构

\Throwable 
    |
    +-- KnotPhpExceptionInterface
    |
    +--[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.
}

回溯

每个KnotPhpException对象都有自己的回溯。因此,您可以通过调用KnotPhpException#getBackTraceList()方法来跟踪调用历史。

try{
    doSomething();
}
catch(KnotPhpException $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.2或更高版本

安装knot-lib/exception

推荐通过Composer安装knot-lib/exception。

composer require knot-lib/exception

安装后,您需要引入Composer的自动加载器

require 'vendor/autoload.php';

许可证

MIT

作者

stk2k

免责声明

本软件无任何保证。

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

请自行承担责任。