diff --git a/README.md b/README.md index 904cea3..1dc4acb 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ -# XXX +# PhpExceptionHelper -XXX \ No newline at end of file +A helper class for exceptions \ No newline at end of file diff --git a/composer.json b/composer.json index 982738d..6739c71 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { - "name": "jrosset/xxx", - "description": "XXX", + "name": "jrosset/exceptionhelper", + "description": "A helper class for exceptions", "keywords": [ ], "minimum-stability": "stable", @@ -15,7 +15,7 @@ }, "readme": "README.md", - "homepage": "https://git.jrosset.ovh/jrosset/XXX", + "homepage": "https://git.jrosset.ovh/jrosset/PhpExceptionHelper", "license": "CC-BY-4.0", "authors": [ { @@ -25,9 +25,9 @@ ], "support": { "email": "jul.rosset@gmail.com", - "issues": "https://git.jrosset.ovh/jrosset/XXX/issues", - "wiki": "https://git.jrosset.ovh/jrosset/XXX/wiki", - "docs": "https://git.jrosset.ovh/jrosset/XXX/wiki", - "source": "https://git.jrosset.ovh/jrosset/XXX" + "issues": "https://git.jrosset.ovh/jrosset/PhpExceptionHelper/issues", + "wiki": "https://git.jrosset.ovh/jrosset/PhpExceptionHelper/wiki", + "docs": "https://git.jrosset.ovh/jrosset/PhpExceptionHelper/wiki", + "source": "https://git.jrosset.ovh/jrosset/PhpExceptionHelper" } } \ No newline at end of file diff --git a/src/ExceptionHelper/ExceptionHelper.php b/src/ExceptionHelper/ExceptionHelper.php new file mode 100644 index 0000000..2a5c541 --- /dev/null +++ b/src/ExceptionHelper/ExceptionHelper.php @@ -0,0 +1,47 @@ +getFile() . ':' . $exception->getLine(); + foreach ($exception->getTrace() as $traceNumber => $traceInfo) { + $traceLines[] = "\t#" . ($traceNumber + 1) + . ' ' . ($traceInfo['class'] ?? '') . ($traceInfo['type'] ?? '') . ($traceInfo['function'] ?? '') + . ' (' . ($traceInfo['file'] ?? '') . ':' . ($traceInfo['line'] ?? '') . ')'; + } + } + + return 'EXCEPTION [' . get_class($exception) . '] : ' . $exception->getMessage() + . ($trace + ? PHP_EOL . implode(PHP_EOL, $traceLines) + : '' + ) + . ($previous && $exception->getPrevious() !== null + ? PHP_EOL . 'Caused by ' . static::toString($exception->getPrevious(), $trace, $previous) + : '' + ); + } +} \ No newline at end of file diff --git a/tests/test.php b/tests/test.php new file mode 100644 index 0000000..0ab5e48 --- /dev/null +++ b/tests/test.php @@ -0,0 +1,20 @@ +