diff --git a/README.md b/README.md index a8ef248..3d48263 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ Original Slim's error handling is bypassed by the HTTP exception manager forcing Non HttpExceptions (for example an exception thrown by a third party library) will be automatically transformed into a 500 HttpException and alternatively you can use `HttpExceptionFactory` to throw HTTP exceptions yourself. Those exceptions will be handled to their corresponding handler depending on their "status code" or by the default handler in case no handler is defined for the specific status code -In the above example if you `throw HttpExceptionFactory::unauthorized()` during the execution of the application it'll be captured by the manager had handed over to "YourUnauthorizedRequestHandler" so you can format and return a proper custom response +In the above example if you `throw HttpExceptionFactory::unauthorized()` during the execution of the application it'll be captured by the manager hand handed over to "YourUnauthorizedRequestHandler" so you can format and return a proper custom response ### HTTP exceptions @@ -73,9 +73,9 @@ The base of this error handling are the HTTP exceptions. This exceptions carry a ```php use Jgut\Slim\Exception\HttpException; -$exceptionCode = 101; // Internal code +$exceptionCode = 1001; // Internal code $httpStatusCode = 401; // Unauthorized -$exception = new HttpException('You shall not pass!', $exceptionCode, $httpStatusCode); +$exception = new HttpException('You shall not pass!', 'You do not have permission', $exceptionCode, $httpStatusCode); $exception->getHttpStatusCode(); // 401 Unauthorized ``` @@ -91,9 +91,9 @@ $exception->getIdentifier(); In order to simplify HTTP exception creation and assure correct HTTP status code selection there are several shortcut creation methods ```php -throw HttpExceptionFactory::unauthorized('You shall not pass!', 101); -throw HttpExceptionFactory::notAcceptable('Throughput reached', 102); -throw HttpExceptionFactory::unprocessableEntity('Already exists', 103); +throw HttpExceptionFactory::unauthorized('You shall not pass!', 'You do not have permission', 1001); +throw HttpExceptionFactory::notAcceptable('Throughput reached', 'Too much', 1002); +throw HttpExceptionFactory::unprocessableEntity('Already exists', 'Entity already exists', 1030); ``` ### Handlers diff --git a/phpstan.neon b/phpstan.neon index 447e8bc..2b7e4e5 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,3 +1,4 @@ parameters: ignoreErrors: - '/Call to an undefined method Throwable::getIdentifier\(\)/' + - '/Call to an undefined method Throwable::getDescription\(\)/' diff --git a/src/Handler/AbstractHttpExceptionHandler.php b/src/Handler/AbstractHttpExceptionHandler.php index 47c0f07..a001998 100644 --- a/src/Handler/AbstractHttpExceptionHandler.php +++ b/src/Handler/AbstractHttpExceptionHandler.php @@ -73,20 +73,7 @@ function (string $contentType) { * * @return array */ - protected function getContentTypes(): array - { - return [ - 'text/plain', - 'text/json', - 'application/json', - 'application/x-json', - 'text/xml', - 'application/xml', - 'application/x-xml', - 'text/html', - 'application/xhtml+xml', - ]; - } + abstract protected function getContentTypes(): array; /** * Get error content. diff --git a/src/Handler/ExceptionHandler.php b/src/Handler/ExceptionHandler.php index c380679..8280162 100644 --- a/src/Handler/ExceptionHandler.php +++ b/src/Handler/ExceptionHandler.php @@ -21,6 +21,24 @@ */ class ExceptionHandler extends AbstractHttpExceptionHandler { + /** + * {@inheritdoc} + */ + protected function getContentTypes(): array + { + return [ + 'text/plain', + 'text/json', + 'application/json', + 'application/x-json', + 'text/xml', + 'application/xml', + 'application/x-xml', + 'text/html', + 'application/xhtml+xml', + ]; + } + /** * {@inheritdoc} */ @@ -45,6 +63,18 @@ protected function getExceptionOutput( return $this->getTextError($exception); } + /** + * Get simple text formatted error. + * + * @param HttpException $exception + * + * @return string + */ + protected function getTextError(HttpException $exception): string + { + return sprintf('(%s) Application error', $exception->getIdentifier()); + } + /** * Get simple JSON formatted error. * @@ -95,16 +125,4 @@ protected function getHtmlError(HttpException $exception): string $exception->getIdentifier() ); } - - /** - * Get simple text formatted error. - * - * @param HttpException $exception - * - * @return string - */ - protected function getTextError(HttpException $exception): string - { - return sprintf('(%s) Application error', $exception->getIdentifier()); - } } diff --git a/src/Handler/MethodNotAllowedHandler.php b/src/Handler/MethodNotAllowedHandler.php index a1219b1..eb789a3 100644 --- a/src/Handler/MethodNotAllowedHandler.php +++ b/src/Handler/MethodNotAllowedHandler.php @@ -14,35 +14,22 @@ namespace Jgut\Slim\Exception\Handler; use Jgut\Slim\Exception\HttpException; -use Psr\Http\Message\ServerRequestInterface; /** * Method not allowed error handler. */ -class MethodNotAllowedHandler extends AbstractHttpExceptionHandler +class MethodNotAllowedHandler extends ExceptionHandler { /** - * {@inheritdoc} + * Get simple text formatted error. + * + * @param HttpException $exception + * + * @return string */ - protected function getExceptionOutput( - string $contentType, - HttpException $exception, - ServerRequestInterface $request - ): string { - if (in_array($contentType, ['text/json', 'application/json', 'application/x-json'], true)) { - return $this->getJsonError($exception); - } - - if (in_array($contentType, ['text/xml', 'application/xml', 'application/x-xml'], true)) { - return $this->getXmlError($exception); - } - - if (in_array($contentType, ['text/html', 'application/xhtml+xml'], true)) { - return $this->getHtmlError($exception); - } - - // text/plain - return $this->getTextError($exception); + protected function getTextError(HttpException $exception): string + { + return sprintf('(%s) %s', $exception->getIdentifier(), $exception->getMessage()); } /** @@ -92,20 +79,9 @@ protected function getHtmlError(HttpException $exception): string '
%s