knot-lib/exception

Exception base class library

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

Description

Exception base class library

Class Hierarchy

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

Note

  • [MUST]You MUST NOT catch Logic Exceptions
  • [MUST]You MUST catch Runtime Exceptions

Interface Hierarchy

\Throwable 
    |
    +-- KnotPhpExceptionInterface
    |
    +--[Package]Logic
    |     |
    |     +-- LogicExceptionInterface
    |
    +--[Package]Runtime
          |
          +-- BusinessExceptionInterface
          |
          +-- ConfigExceptionInterface
          |
          +-- RuntimeExceptionInterface

Note

  • You can catch exceptions by interface, instead of catching by concrete exception class names.
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.
}

Back Traces

Every KnotPhpException objects have own backtraces. So you can trace the call history by calling KnotPhpException#getBackTraceList() method.

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;
    });
}

Requirement

PHP 7.2 or later

Installing knot-lib/exception

The recommended way to install knot-lib/exception is through Composer.

composer require knot-lib/exception

After installing, you need to require Composer's autoloader:

require 'vendor/autoload.php';

License

MIT

Author

stk2k

Disclaimer

This software is no warranty.

We are not responsible for any results caused by the use of this software.

Please use the responsibility of the your self.