Extended Exceptions interface
For more detailed Exception handling and reporting
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
vendor
|
||||
composer.lock
|
||||
24
composer.json
Normal file
24
composer.json
Normal 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",
|
||||
},
|
||||
}
|
||||
43
src/Exceptions/Abstracts/CustomException.php
Normal file
43
src/Exceptions/Abstracts/CustomException.php
Normal 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__;
|
||||
77
src/Exceptions/Abstracts/Interface/IException.php
Normal file
77
src/Exceptions/Abstracts/Interface/IException.php
Normal 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__
|
||||
Reference in New Issue
Block a user