Extended Exceptions interface

For more detailed Exception handling and reporting
This commit is contained in:
2023-09-01 08:52:37 +09:00
commit d98fd2c295
4 changed files with 146 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
vendor
composer.lock

24
composer.json Normal file
View File

@@ -0,0 +1,24 @@
{
"name": "egrajp/exceptions-extended",
"description": "Extended Exceptions based on the default PHP Exceptions",
"type": "library",
"license": "MIT",
"keywords": [
"exceptions"
],
"authors": [
{
"name": "Clemens Schwaighofer",
"email": "clemens.schwaighofer@egplusww.com"
}
],
"autoload": {
"classmap": [
"src/"
]
},
"minimum-stability": "dev",
"require": {
"php": ">=8.2",
},
}

View File

@@ -0,0 +1,43 @@
<?php
namespace Exceptions\Abstracts;
abstract class CustomException extends \Exception implements Interface\IException
{
/**
* constructor that creates empty basic message if no message is set
*
* @param string $message
* @param int $code
* @param \Throwable|null $previous
*/
public function __construct(string $message = '', int $code = 0, ?\Throwable $previous = null)
{
if (empty($message)) {
throw new $this('Unknown ' . get_class($this), $code, $previous);
}
parent::__construct($message, $code, $previous);
}
/**
* add more information to the return exception besides basic info
*
* @return string
*/
public function __toString()
{
// chain previous ones (message/code/file/etc)
$previous = $this->getPrevious();
return get_class($this)
. " '{$this->getMessage()}' ({$this->getCode()}) in "
. "{$this->getFile()}:{$this->getLine()}\n"
. "{$this->getTraceAsString()}"
. ($previous !== null ?
"\n"
. "Previous:\n{$this->getPrevious()}" :
''
);
}
}
// __END__;

View File

@@ -0,0 +1,77 @@
<?php
namespace Exceptions\Abstracts\Interface;
interface IException
{
/* Protected methods inherited from Exception class */
/**
* Exception message
*
* @return string
*/
public function getMessage(): string;
/**
* Get previous exceptions
*
* @return \Throwable|null
*/
public function getPrevious(): ?\Throwable;
/**
* User-defined Exception code
*
* @return int
*/
public function getCode(): int;
/**
* Source filename
*
* @return string
*/
public function getFile(): string;
/**
* Source line
*
* @return int
*/
public function getLine(): int;
/**
* An array of the backtrace()
*
* @return array<array<mixed>>
*/
public function getTrace(): array;
/**
* Formated string of trace
*
* @return string
*/
public function getTraceAsString(): string;
/* Overrideable methods inherited from Exception class */
/**
* formated string for display
*
* @return string
*/
public function __toString(): string;
/**
* Undocumented function
*
* @param string $message
* @param int $code
* @param \Throwable|null $previous
*/
public function __construct(string $message = '', int $code = 0, ?\Throwable $previous = null);
}
// __END__