Skip to content

Commit

Permalink
Added localized descriptions to instances of \Slim\Exception\Http*Exc…
Browse files Browse the repository at this point in the history
…eption thrown where possible
  • Loading branch information
rotimi committed Feb 24, 2025
1 parent a9f99f7 commit 260ba9e
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 13 deletions.
34 changes: 28 additions & 6 deletions src/MvcRouteHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),

Check failure on line 95 in src/MvcRouteHandler.php

View workflow job for this annotation

GitHub Actions / PHP-8.4 - prefer-stable - ubuntu-latest

PossiblyNullArgument

src/MvcRouteHandler.php:95:17: PossiblyNullArgument: Argument 1 of SlimMvcTools\Utils::createSlimHttpExceptionWithLocalizedDescription cannot be null, possibly null value provided (see https://psalm.dev/078)

Check failure on line 95 in src/MvcRouteHandler.php

View workflow job for this annotation

GitHub Actions / PHP-8.3 - prefer-stable - ubuntu-latest

PossiblyNullArgument

src/MvcRouteHandler.php:95:17: PossiblyNullArgument: Argument 1 of SlimMvcTools\Utils::createSlimHttpExceptionWithLocalizedDescription cannot be null, possibly null value provided (see https://psalm.dev/078)

Check failure on line 95 in src/MvcRouteHandler.php

View workflow job for this annotation

GitHub Actions / PHP-8.2 - prefer-stable - ubuntu-latest

PossiblyNullArgument

src/MvcRouteHandler.php:95:17: PossiblyNullArgument: Argument 1 of SlimMvcTools\Utils::createSlimHttpExceptionWithLocalizedDescription cannot be null, possibly null value provided (see https://psalm.dev/078)

Check failure on line 95 in src/MvcRouteHandler.php

View workflow job for this annotation

GitHub Actions / PHP-8.1 - prefer-stable - ubuntu-latest

PossiblyNullArgument

src/MvcRouteHandler.php:95:17: PossiblyNullArgument: Argument 1 of SlimMvcTools\Utils::createSlimHttpExceptionWithLocalizedDescription cannot be null, possibly null value provided (see https://psalm.dev/078)
SlimHttpExceptionClassNames::HttpBadRequestException,
$req,
$log_message,
$e
);

} catch (\Throwable $e) {

Expand All @@ -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(),

Check failure on line 111 in src/MvcRouteHandler.php

View workflow job for this annotation

GitHub Actions / PHP-8.4 - prefer-stable - ubuntu-latest

PossiblyNullArgument

src/MvcRouteHandler.php:111:17: PossiblyNullArgument: Argument 1 of SlimMvcTools\Utils::createSlimHttpExceptionWithLocalizedDescription cannot be null, possibly null value provided (see https://psalm.dev/078)

Check failure on line 111 in src/MvcRouteHandler.php

View workflow job for this annotation

GitHub Actions / PHP-8.3 - prefer-stable - ubuntu-latest

PossiblyNullArgument

src/MvcRouteHandler.php:111:17: PossiblyNullArgument: Argument 1 of SlimMvcTools\Utils::createSlimHttpExceptionWithLocalizedDescription cannot be null, possibly null value provided (see https://psalm.dev/078)

Check failure on line 111 in src/MvcRouteHandler.php

View workflow job for this annotation

GitHub Actions / PHP-8.2 - prefer-stable - ubuntu-latest

PossiblyNullArgument

src/MvcRouteHandler.php:111:17: PossiblyNullArgument: Argument 1 of SlimMvcTools\Utils::createSlimHttpExceptionWithLocalizedDescription cannot be null, possibly null value provided (see https://psalm.dev/078)

Check failure on line 111 in src/MvcRouteHandler.php

View workflow job for this annotation

GitHub Actions / PHP-8.1 - prefer-stable - ubuntu-latest

PossiblyNullArgument

src/MvcRouteHandler.php:111:17: PossiblyNullArgument: Argument 1 of SlimMvcTools\Utils::createSlimHttpExceptionWithLocalizedDescription cannot be null, possibly null value provided (see https://psalm.dev/078)
SlimHttpExceptionClassNames::HttpInternalServerErrorException,
$req,
$log_message,
$e
);
}

// If we got this far, that means that the action method was successfully
Expand Down Expand Up @@ -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(),

Check failure on line 154 in src/MvcRouteHandler.php

View workflow job for this annotation

GitHub Actions / PHP-8.4 - prefer-stable - ubuntu-latest

PossiblyNullArgument

src/MvcRouteHandler.php:154:17: PossiblyNullArgument: Argument 1 of SlimMvcTools\Utils::createSlimHttpExceptionWithLocalizedDescription cannot be null, possibly null value provided (see https://psalm.dev/078)

Check failure on line 154 in src/MvcRouteHandler.php

View workflow job for this annotation

GitHub Actions / PHP-8.3 - prefer-stable - ubuntu-latest

PossiblyNullArgument

src/MvcRouteHandler.php:154:17: PossiblyNullArgument: Argument 1 of SlimMvcTools\Utils::createSlimHttpExceptionWithLocalizedDescription cannot be null, possibly null value provided (see https://psalm.dev/078)

Check failure on line 154 in src/MvcRouteHandler.php

View workflow job for this annotation

GitHub Actions / PHP-8.2 - prefer-stable - ubuntu-latest

PossiblyNullArgument

src/MvcRouteHandler.php:154:17: PossiblyNullArgument: Argument 1 of SlimMvcTools\Utils::createSlimHttpExceptionWithLocalizedDescription cannot be null, possibly null value provided (see https://psalm.dev/078)

Check failure on line 154 in src/MvcRouteHandler.php

View workflow job for this annotation

GitHub Actions / PHP-8.1 - prefer-stable - ubuntu-latest

PossiblyNullArgument

src/MvcRouteHandler.php:154:17: PossiblyNullArgument: Argument 1 of SlimMvcTools\Utils::createSlimHttpExceptionWithLocalizedDescription cannot be null, possibly null value provided (see https://psalm.dev/078)
SlimHttpExceptionClassNames::HttpBadRequestException,
$req,
$err_message
);
}
}

Expand All @@ -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(),

Check failure on line 178 in src/MvcRouteHandler.php

View workflow job for this annotation

GitHub Actions / PHP-8.4 - prefer-stable - ubuntu-latest

PossiblyNullArgument

src/MvcRouteHandler.php:178:17: PossiblyNullArgument: Argument 1 of SlimMvcTools\Utils::createSlimHttpExceptionWithLocalizedDescription cannot be null, possibly null value provided (see https://psalm.dev/078)

Check failure on line 178 in src/MvcRouteHandler.php

View workflow job for this annotation

GitHub Actions / PHP-8.3 - prefer-stable - ubuntu-latest

PossiblyNullArgument

src/MvcRouteHandler.php:178:17: PossiblyNullArgument: Argument 1 of SlimMvcTools\Utils::createSlimHttpExceptionWithLocalizedDescription cannot be null, possibly null value provided (see https://psalm.dev/078)

Check failure on line 178 in src/MvcRouteHandler.php

View workflow job for this annotation

GitHub Actions / PHP-8.2 - prefer-stable - ubuntu-latest

PossiblyNullArgument

src/MvcRouteHandler.php:178:17: PossiblyNullArgument: Argument 1 of SlimMvcTools\Utils::createSlimHttpExceptionWithLocalizedDescription cannot be null, possibly null value provided (see https://psalm.dev/078)

Check failure on line 178 in src/MvcRouteHandler.php

View workflow job for this annotation

GitHub Actions / PHP-8.1 - prefer-stable - ubuntu-latest

PossiblyNullArgument

src/MvcRouteHandler.php:178:17: PossiblyNullArgument: Argument 1 of SlimMvcTools\Utils::createSlimHttpExceptionWithLocalizedDescription cannot be null, possibly null value provided (see https://psalm.dev/078)
SlimHttpExceptionClassNames::HttpNotFoundException,
$req,
$err_message
);
}
}
}
21 changes: 21 additions & 0 deletions src/SlimHttpExceptionClassNames.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace SlimMvcTools;

/**
*
* @author rotimi
*/
enum SlimHttpExceptionClassNames: string {

case HttpBadRequestException = \Slim\Exception\HttpBadRequestException::class;
case HttpForbiddenException = \Slim\Exception\HttpForbiddenException::class;
case HttpGoneException = \Slim\Exception\HttpGoneException::class;
case HttpInternalServerErrorException = \Slim\Exception\HttpInternalServerErrorException::class;
case HttpMethodNotAllowedException = \Slim\Exception\HttpMethodNotAllowedException::class;
case HttpNotFoundException = \Slim\Exception\HttpNotFoundException::class;
case HttpNotImplementedException = \Slim\Exception\HttpNotImplementedException::class;
case HttpTooManyRequestsException = \Slim\Exception\HttpTooManyRequestsException::class;
case HttpUnauthorizedException = \Slim\Exception\HttpUnauthorizedException::class;
}

24 changes: 24 additions & 0 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,28 @@ public static function getThrowableAsStr(Throwable $e, string $eol=PHP_EOL): str

return $message;
}

public static function createSlimHttpExceptionWithLocalizedDescription(
\Psr\Container\ContainerInterface $container,
SlimHttpExceptionClassNames $exception_class,
\Psr\Http\Message\RequestInterface $req,
string $err_message,
?\Throwable $previous_exception = null
): \Slim\Exception\HttpSpecializedException {

$exception_class_name = $exception_class->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)

Check failure on line 50 in src/Utils.php

View workflow job for this annotation

GitHub Actions / PHP-8.4 - prefer-stable - ubuntu-latest

MixedArgument

src/Utils.php:50:21: MixedArgument: Argument 1 of Slim\Exception\HttpUnauthorizedException::setDescription cannot be mixed, expecting string (see https://psalm.dev/030)

Check failure on line 50 in src/Utils.php

View workflow job for this annotation

GitHub Actions / PHP-8.4 - prefer-stable - ubuntu-latest

MixedArgument

src/Utils.php:50:21: MixedArgument: Argument 1 of Slim\Exception\HttpBadRequestException::setDescription cannot be mixed, expecting string (see https://psalm.dev/030)

Check failure on line 50 in src/Utils.php

View workflow job for this annotation

GitHub Actions / PHP-8.4 - prefer-stable - ubuntu-latest

MixedArgument

src/Utils.php:50:21: MixedArgument: Argument 1 of Slim\Exception\HttpForbiddenException::setDescription cannot be mixed, expecting string (see https://psalm.dev/030)

Check failure on line 50 in src/Utils.php

View workflow job for this annotation

GitHub Actions / PHP-8.4 - prefer-stable - ubuntu-latest

MixedArgument

src/Utils.php:50:21: MixedArgument: Argument 1 of Slim\Exception\HttpGoneException::setDescription cannot be mixed, expecting string (see https://psalm.dev/030)

Check failure on line 50 in src/Utils.php

View workflow job for this annotation

GitHub Actions / PHP-8.4 - prefer-stable - ubuntu-latest

MixedArgument

src/Utils.php:50:21: MixedArgument: Argument 1 of Slim\Exception\HttpInternalServerErrorException::setDescription cannot be mixed, expecting string (see https://psalm.dev/030)

Check failure on line 50 in src/Utils.php

View workflow job for this annotation

GitHub Actions / PHP-8.4 - prefer-stable - ubuntu-latest

MixedArgument

src/Utils.php:50:21: MixedArgument: Argument 1 of Slim\Exception\HttpMethodNotAllowedException::setDescription cannot be mixed, expecting string (see https://psalm.dev/030)

Check failure on line 50 in src/Utils.php

View workflow job for this annotation

GitHub Actions / PHP-8.3 - prefer-stable - ubuntu-latest

MixedArgument

src/Utils.php:50:21: MixedArgument: Argument 1 of Slim\Exception\HttpUnauthorizedException::setDescription cannot be mixed, expecting string (see https://psalm.dev/030)

Check failure on line 50 in src/Utils.php

View workflow job for this annotation

GitHub Actions / PHP-8.3 - prefer-stable - ubuntu-latest

MixedArgument

src/Utils.php:50:21: MixedArgument: Argument 1 of Slim\Exception\HttpBadRequestException::setDescription cannot be mixed, expecting string (see https://psalm.dev/030)

Check failure on line 50 in src/Utils.php

View workflow job for this annotation

GitHub Actions / PHP-8.3 - prefer-stable - ubuntu-latest

MixedArgument

src/Utils.php:50:21: MixedArgument: Argument 1 of Slim\Exception\HttpForbiddenException::setDescription cannot be mixed, expecting string (see https://psalm.dev/030)

Check failure on line 50 in src/Utils.php

View workflow job for this annotation

GitHub Actions / PHP-8.3 - prefer-stable - ubuntu-latest

MixedArgument

src/Utils.php:50:21: MixedArgument: Argument 1 of Slim\Exception\HttpGoneException::setDescription cannot be mixed, expecting string (see https://psalm.dev/030)

Check failure on line 50 in src/Utils.php

View workflow job for this annotation

GitHub Actions / PHP-8.3 - prefer-stable - ubuntu-latest

MixedArgument

src/Utils.php:50:21: MixedArgument: Argument 1 of Slim\Exception\HttpInternalServerErrorException::setDescription cannot be mixed, expecting string (see https://psalm.dev/030)

Check failure on line 50 in src/Utils.php

View workflow job for this annotation

GitHub Actions / PHP-8.3 - prefer-stable - ubuntu-latest

MixedArgument

src/Utils.php:50:21: MixedArgument: Argument 1 of Slim\Exception\HttpMethodNotAllowedException::setDescription cannot be mixed, expecting string (see https://psalm.dev/030)

Check failure on line 50 in src/Utils.php

View workflow job for this annotation

GitHub Actions / PHP-8.2 - prefer-stable - ubuntu-latest

MixedArgument

src/Utils.php:50:21: MixedArgument: Argument 1 of Slim\Exception\HttpUnauthorizedException::setDescription cannot be mixed, expecting string (see https://psalm.dev/030)

Check failure on line 50 in src/Utils.php

View workflow job for this annotation

GitHub Actions / PHP-8.2 - prefer-stable - ubuntu-latest

MixedArgument

src/Utils.php:50:21: MixedArgument: Argument 1 of Slim\Exception\HttpBadRequestException::setDescription cannot be mixed, expecting string (see https://psalm.dev/030)

Check failure on line 50 in src/Utils.php

View workflow job for this annotation

GitHub Actions / PHP-8.2 - prefer-stable - ubuntu-latest

MixedArgument

src/Utils.php:50:21: MixedArgument: Argument 1 of Slim\Exception\HttpForbiddenException::setDescription cannot be mixed, expecting string (see https://psalm.dev/030)

Check failure on line 50 in src/Utils.php

View workflow job for this annotation

GitHub Actions / PHP-8.2 - prefer-stable - ubuntu-latest

MixedArgument

src/Utils.php:50:21: MixedArgument: Argument 1 of Slim\Exception\HttpGoneException::setDescription cannot be mixed, expecting string (see https://psalm.dev/030)

Check failure on line 50 in src/Utils.php

View workflow job for this annotation

GitHub Actions / PHP-8.2 - prefer-stable - ubuntu-latest

MixedArgument

src/Utils.php:50:21: MixedArgument: Argument 1 of Slim\Exception\HttpInternalServerErrorException::setDescription cannot be mixed, expecting string (see https://psalm.dev/030)

Check failure on line 50 in src/Utils.php

View workflow job for this annotation

GitHub Actions / PHP-8.2 - prefer-stable - ubuntu-latest

MixedArgument

src/Utils.php:50:21: MixedArgument: Argument 1 of Slim\Exception\HttpMethodNotAllowedException::setDescription cannot be mixed, expecting string (see https://psalm.dev/030)

Check failure on line 50 in src/Utils.php

View workflow job for this annotation

GitHub Actions / PHP-8.1 - prefer-stable - ubuntu-latest

MixedArgument

src/Utils.php:50:21: MixedArgument: Argument 1 of Slim\Exception\HttpUnauthorizedException::setDescription cannot be mixed, expecting string (see https://psalm.dev/030)

Check failure on line 50 in src/Utils.php

View workflow job for this annotation

GitHub Actions / PHP-8.1 - prefer-stable - ubuntu-latest

MixedArgument

src/Utils.php:50:21: MixedArgument: Argument 1 of Slim\Exception\HttpBadRequestException::setDescription cannot be mixed, expecting string (see https://psalm.dev/030)

Check failure on line 50 in src/Utils.php

View workflow job for this annotation

GitHub Actions / PHP-8.1 - prefer-stable - ubuntu-latest

MixedArgument

src/Utils.php:50:21: MixedArgument: Argument 1 of Slim\Exception\HttpForbiddenException::setDescription cannot be mixed, expecting string (see https://psalm.dev/030)

Check failure on line 50 in src/Utils.php

View workflow job for this annotation

GitHub Actions / PHP-8.1 - prefer-stable - ubuntu-latest

MixedArgument

src/Utils.php:50:21: MixedArgument: Argument 1 of Slim\Exception\HttpGoneException::setDescription cannot be mixed, expecting string (see https://psalm.dev/030)

Check failure on line 50 in src/Utils.php

View workflow job for this annotation

GitHub Actions / PHP-8.1 - prefer-stable - ubuntu-latest

MixedArgument

src/Utils.php:50:21: MixedArgument: Argument 1 of Slim\Exception\HttpInternalServerErrorException::setDescription cannot be mixed, expecting string (see https://psalm.dev/030)

Check failure on line 50 in src/Utils.php

View workflow job for this annotation

GitHub Actions / PHP-8.1 - prefer-stable - ubuntu-latest

MixedArgument

src/Utils.php:50:21: MixedArgument: Argument 1 of Slim\Exception\HttpMethodNotAllowedException::setDescription cannot be mixed, expecting string (see https://psalm.dev/030)
->gettext($exception_class->value.'_description')
);
}

return $exception;
}
}
19 changes: 16 additions & 3 deletions src/controllers/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}

Expand Down Expand Up @@ -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
*/
Expand Down
25 changes: 21 additions & 4 deletions src/functions/framework-helpers.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
declare(strict_types=1);

use \SlimMvcTools\ContainerKeys;
use \SlimMvcTools\{ContainerKeys, Utils, SlimHttpExceptionClassNames};

/**
* Creates & returns a controller object that is an instance of
Expand Down Expand Up @@ -31,7 +31,13 @@ function sMVC_CreateController(
//any number of letters, numbers, or underscores.
/** @psalm-suppress InvalidOperand */
$extra_log_message = "`" . __FILE__ . "` on line " . __LINE__ . ": Bad controller name `{$controller_class_name}`";
throw new \Slim\Exception\HttpBadRequestException($request, $extra_log_message);

throw Utils::createSlimHttpExceptionWithLocalizedDescription(
$container,
SlimHttpExceptionClassNames::HttpBadRequestException,
$request,
$extra_log_message
);
}

if( !class_exists($controller_class_name) ) {
Expand Down Expand Up @@ -61,7 +67,12 @@ function sMVC_CreateController(
//404 Not Found: Controller class not found.
/** @psalm-suppress InvalidOperand */
$extra_log_message = "`".__FILE__."` on line ".__LINE__.": Class `{$controller_class_name}` does not exist.";
throw new \Slim\Exception\HttpNotFoundException($request, $extra_log_message);
throw Utils::createSlimHttpExceptionWithLocalizedDescription(
$container,
SlimHttpExceptionClassNames::HttpNotFoundException,
$request,
$extra_log_message
);
}
}

Expand All @@ -73,7 +84,13 @@ function sMVC_CreateController(
"`".__FILE__."` on line ".__LINE__
. sprintf(': `%s` could not be mapped to a valid controller.', $request->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
Expand Down

0 comments on commit 260ba9e

Please sign in to comment.