From d98fd2c2952113a072060086481d2241d72d728d Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Fri, 1 Sep 2023 08:52:37 +0900 Subject: [PATCH] Extended Exceptions interface For more detailed Exception handling and reporting --- .gitignore | 2 + composer.json | 24 ++++++ src/Exceptions/Abstracts/CustomException.php | 43 +++++++++++ .../Abstracts/Interface/IException.php | 77 +++++++++++++++++++ 4 files changed, 146 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 src/Exceptions/Abstracts/CustomException.php create mode 100644 src/Exceptions/Abstracts/Interface/IException.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7579f74 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +vendor +composer.lock diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..b1c8db5 --- /dev/null +++ b/composer.json @@ -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", + }, +} diff --git a/src/Exceptions/Abstracts/CustomException.php b/src/Exceptions/Abstracts/CustomException.php new file mode 100644 index 0000000..414586d --- /dev/null +++ b/src/Exceptions/Abstracts/CustomException.php @@ -0,0 +1,43 @@ +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__; diff --git a/src/Exceptions/Abstracts/Interface/IException.php b/src/Exceptions/Abstracts/Interface/IException.php new file mode 100644 index 0000000..528edb6 --- /dev/null +++ b/src/Exceptions/Abstracts/Interface/IException.php @@ -0,0 +1,77 @@ +> + */ + 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__