From 260ba9ea706a86e9240fa59a78448f1a4f9c434b Mon Sep 17 00:00:00 2001 From: rotimi Date: Mon, 24 Feb 2025 01:29:47 -0700 Subject: [PATCH] Added localized descriptions to instances of \Slim\Exception\Http*Exception thrown where possible --- src/MvcRouteHandler.php | 34 ++++++++++++++++++++++++----- src/SlimHttpExceptionClassNames.php | 21 ++++++++++++++++++ src/Utils.php | 24 ++++++++++++++++++++ src/controllers/BaseController.php | 19 +++++++++++++--- src/functions/framework-helpers.php | 25 +++++++++++++++++---- 5 files changed, 110 insertions(+), 13 deletions(-) create mode 100644 src/SlimHttpExceptionClassNames.php diff --git a/src/MvcRouteHandler.php b/src/MvcRouteHandler.php index 5b89f3c..a270c65 100644 --- a/src/MvcRouteHandler.php +++ b/src/MvcRouteHandler.php @@ -91,7 +91,13 @@ public function __invoke( "`".__FILE__."` on line ".__LINE__ . sprintf(': Not enough arguments when calling `%s`(...) on an instance of `%s` for the uri `%s`.', $action_method, $controller_obj::class, $req->getUri()->__toString()); - throw new \Slim\Exception\HttpBadRequestException($req, $log_message, $e); + throw Utils::createSlimHttpExceptionWithLocalizedDescription( + $this->app->getContainer(), + SlimHttpExceptionClassNames::HttpBadRequestException, + $req, + $log_message, + $e + ); } catch (\Throwable $e) { @@ -100,8 +106,14 @@ public function __invoke( $log_message = "`".__FILE__."` on line ".__LINE__ . sprintf(': Error occured when calling `%s`(...) on an instance of `%s` for the uri `%s`.', $action_method, $controller_obj::class, $req->getUri()->__toString()); - - throw new \Slim\Exception\HttpInternalServerErrorException($req, $log_message, $e); + + throw Utils::createSlimHttpExceptionWithLocalizedDescription( + $this->app->getContainer(), + SlimHttpExceptionClassNames::HttpInternalServerErrorException, + $req, + $log_message, + $e + ); } // If we got this far, that means that the action method was successfully @@ -138,7 +150,12 @@ protected function validateMethodName( /** @psalm-suppress InvalidOperand */ $err_message = "`".__FILE__."` on line ".__LINE__.": Bad action name `{$action_method}`."; - throw new \Slim\Exception\HttpBadRequestException($req, $err_message); + throw Utils::createSlimHttpExceptionWithLocalizedDescription( + $this->app->getContainer(), + SlimHttpExceptionClassNames::HttpBadRequestException, + $req, + $err_message + ); } } @@ -156,8 +173,13 @@ protected function assertMethodExistsOnControllerObj( /** @psalm-suppress InvalidOperand */ $err_message = "`".__FILE__."` on line ".__LINE__ .": The action method `{$action_method}` does not exist in class `{$controller_class_name}`."; - - throw new \Slim\Exception\HttpNotFoundException($req, $err_message); + + throw Utils::createSlimHttpExceptionWithLocalizedDescription( + $this->app->getContainer(), + SlimHttpExceptionClassNames::HttpNotFoundException, + $req, + $err_message + ); } } } diff --git a/src/SlimHttpExceptionClassNames.php b/src/SlimHttpExceptionClassNames.php new file mode 100644 index 0000000..0cfa16e --- /dev/null +++ b/src/SlimHttpExceptionClassNames.php @@ -0,0 +1,21 @@ +value; + $exception = new $exception_class_name($req, $err_message, $previous_exception); + + if( + $container->has(ContainerKeys::LOCALE_OBJ) + && $container->get(ContainerKeys::LOCALE_OBJ) instanceof \Vespula\Locale\Locale + ) { + $exception->setDescription( + $container->get(ContainerKeys::LOCALE_OBJ) + ->gettext($exception_class->value.'_description') + ); + } + + return $exception; + } } diff --git a/src/controllers/BaseController.php b/src/controllers/BaseController.php index 4ac435e..4bdcabc 100644 --- a/src/controllers/BaseController.php +++ b/src/controllers/BaseController.php @@ -1063,8 +1063,13 @@ public function getContainerItem(string $item_key_in_container) { $msg = "ERROR: The item with the key named `$item_key_in_container` does not exist in" . " the container associated with `" . static::class . "` ." . PHP_EOL; - - throw new \Slim\Exception\HttpInternalServerErrorException($this->request, $msg); + + throw Utils::createSlimHttpExceptionWithLocalizedDescription( + $this->getContainer(), + \SlimMvcTools\SlimHttpExceptionClassNames::HttpInternalServerErrorException, + $this->request, + $msg + ); } } @@ -1120,7 +1125,15 @@ public function forceHttp410(string $message, ?ServerRequestInterface $request=n throw (new \Slim\Exception\HttpGoneException(($request ?? $this->request), $message))->setDescription($message); } - + + /** + * @psalm-suppress PossiblyUnusedMethod + */ + public function forceHttp429(string $message, ?ServerRequestInterface $request=null): void { + + throw (new \Slim\Exception\HttpTooManyRequestsException(($request ?? $this->request), $message))->setDescription($message); + } + /** * @psalm-suppress PossiblyUnusedMethod */ diff --git a/src/functions/framework-helpers.php b/src/functions/framework-helpers.php index 96a03c8..2a3cc0d 100644 --- a/src/functions/framework-helpers.php +++ b/src/functions/framework-helpers.php @@ -1,7 +1,7 @@ getUri()->__toString()); - throw new \Slim\Exception\HttpBadRequestException($request, $extra_log_message); + //throw new \Slim\Exception\HttpBadRequestException($request, $extra_log_message); + throw Utils::createSlimHttpExceptionWithLocalizedDescription( + $container, + SlimHttpExceptionClassNames::HttpBadRequestException, + $request, + $extra_log_message + ); } //Create the controller object