Skip to content

Commit 260ba9e

Browse files
author
rotimi
committed
Added localized descriptions to instances of \Slim\Exception\Http*Exception thrown where possible
1 parent a9f99f7 commit 260ba9e

File tree

5 files changed

+110
-13
lines changed

5 files changed

+110
-13
lines changed

src/MvcRouteHandler.php

+28-6
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,13 @@ public function __invoke(
9191
"`".__FILE__."` on line ".__LINE__
9292
. sprintf(': Not enough arguments when calling `%s`(...) on an instance of `%s` for the uri `%s`.', $action_method, $controller_obj::class, $req->getUri()->__toString());
9393

94-
throw new \Slim\Exception\HttpBadRequestException($req, $log_message, $e);
94+
throw Utils::createSlimHttpExceptionWithLocalizedDescription(
95+
$this->app->getContainer(),
96+
SlimHttpExceptionClassNames::HttpBadRequestException,
97+
$req,
98+
$log_message,
99+
$e
100+
);
95101

96102
} catch (\Throwable $e) {
97103

@@ -100,8 +106,14 @@ public function __invoke(
100106
$log_message =
101107
"`".__FILE__."` on line ".__LINE__
102108
. sprintf(': Error occured when calling `%s`(...) on an instance of `%s` for the uri `%s`.', $action_method, $controller_obj::class, $req->getUri()->__toString());
103-
104-
throw new \Slim\Exception\HttpInternalServerErrorException($req, $log_message, $e);
109+
110+
throw Utils::createSlimHttpExceptionWithLocalizedDescription(
111+
$this->app->getContainer(),
112+
SlimHttpExceptionClassNames::HttpInternalServerErrorException,
113+
$req,
114+
$log_message,
115+
$e
116+
);
105117
}
106118

107119
// If we got this far, that means that the action method was successfully
@@ -138,7 +150,12 @@ protected function validateMethodName(
138150
/** @psalm-suppress InvalidOperand */
139151
$err_message = "`".__FILE__."` on line ".__LINE__.": Bad action name `{$action_method}`.";
140152

141-
throw new \Slim\Exception\HttpBadRequestException($req, $err_message);
153+
throw Utils::createSlimHttpExceptionWithLocalizedDescription(
154+
$this->app->getContainer(),
155+
SlimHttpExceptionClassNames::HttpBadRequestException,
156+
$req,
157+
$err_message
158+
);
142159
}
143160
}
144161

@@ -156,8 +173,13 @@ protected function assertMethodExistsOnControllerObj(
156173
/** @psalm-suppress InvalidOperand */
157174
$err_message = "`".__FILE__."` on line ".__LINE__
158175
.": The action method `{$action_method}` does not exist in class `{$controller_class_name}`.";
159-
160-
throw new \Slim\Exception\HttpNotFoundException($req, $err_message);
176+
177+
throw Utils::createSlimHttpExceptionWithLocalizedDescription(
178+
$this->app->getContainer(),
179+
SlimHttpExceptionClassNames::HttpNotFoundException,
180+
$req,
181+
$err_message
182+
);
161183
}
162184
}
163185
}

src/SlimHttpExceptionClassNames.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace SlimMvcTools;
4+
5+
/**
6+
*
7+
* @author rotimi
8+
*/
9+
enum SlimHttpExceptionClassNames: string {
10+
11+
case HttpBadRequestException = \Slim\Exception\HttpBadRequestException::class;
12+
case HttpForbiddenException = \Slim\Exception\HttpForbiddenException::class;
13+
case HttpGoneException = \Slim\Exception\HttpGoneException::class;
14+
case HttpInternalServerErrorException = \Slim\Exception\HttpInternalServerErrorException::class;
15+
case HttpMethodNotAllowedException = \Slim\Exception\HttpMethodNotAllowedException::class;
16+
case HttpNotFoundException = \Slim\Exception\HttpNotFoundException::class;
17+
case HttpNotImplementedException = \Slim\Exception\HttpNotImplementedException::class;
18+
case HttpTooManyRequestsException = \Slim\Exception\HttpTooManyRequestsException::class;
19+
case HttpUnauthorizedException = \Slim\Exception\HttpUnauthorizedException::class;
20+
}
21+

src/Utils.php

+24
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,28 @@ public static function getThrowableAsStr(Throwable $e, string $eol=PHP_EOL): str
3030

3131
return $message;
3232
}
33+
34+
public static function createSlimHttpExceptionWithLocalizedDescription(
35+
\Psr\Container\ContainerInterface $container,
36+
SlimHttpExceptionClassNames $exception_class,
37+
\Psr\Http\Message\RequestInterface $req,
38+
string $err_message,
39+
?\Throwable $previous_exception = null
40+
): \Slim\Exception\HttpSpecializedException {
41+
42+
$exception_class_name = $exception_class->value;
43+
$exception = new $exception_class_name($req, $err_message, $previous_exception);
44+
45+
if(
46+
$container->has(ContainerKeys::LOCALE_OBJ)
47+
&& $container->get(ContainerKeys::LOCALE_OBJ) instanceof \Vespula\Locale\Locale
48+
) {
49+
$exception->setDescription(
50+
$container->get(ContainerKeys::LOCALE_OBJ)
51+
->gettext($exception_class->value.'_description')
52+
);
53+
}
54+
55+
return $exception;
56+
}
3357
}

src/controllers/BaseController.php

+16-3
Original file line numberDiff line numberDiff line change
@@ -1063,8 +1063,13 @@ public function getContainerItem(string $item_key_in_container) {
10631063
$msg = "ERROR: The item with the key named `$item_key_in_container` does not exist in"
10641064
. " the container associated with `" . static::class . "` ."
10651065
. PHP_EOL;
1066-
1067-
throw new \Slim\Exception\HttpInternalServerErrorException($this->request, $msg);
1066+
1067+
throw Utils::createSlimHttpExceptionWithLocalizedDescription(
1068+
$this->getContainer(),
1069+
\SlimMvcTools\SlimHttpExceptionClassNames::HttpInternalServerErrorException,
1070+
$this->request,
1071+
$msg
1072+
);
10681073
}
10691074
}
10701075

@@ -1120,7 +1125,15 @@ public function forceHttp410(string $message, ?ServerRequestInterface $request=n
11201125

11211126
throw (new \Slim\Exception\HttpGoneException(($request ?? $this->request), $message))->setDescription($message);
11221127
}
1123-
1128+
1129+
/**
1130+
* @psalm-suppress PossiblyUnusedMethod
1131+
*/
1132+
public function forceHttp429(string $message, ?ServerRequestInterface $request=null): void {
1133+
1134+
throw (new \Slim\Exception\HttpTooManyRequestsException(($request ?? $this->request), $message))->setDescription($message);
1135+
}
1136+
11241137
/**
11251138
* @psalm-suppress PossiblyUnusedMethod
11261139
*/

src/functions/framework-helpers.php

+21-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
declare(strict_types=1);
33

4-
use \SlimMvcTools\ContainerKeys;
4+
use \SlimMvcTools\{ContainerKeys, Utils, SlimHttpExceptionClassNames};
55

66
/**
77
* Creates & returns a controller object that is an instance of
@@ -31,7 +31,13 @@ function sMVC_CreateController(
3131
//any number of letters, numbers, or underscores.
3232
/** @psalm-suppress InvalidOperand */
3333
$extra_log_message = "`" . __FILE__ . "` on line " . __LINE__ . ": Bad controller name `{$controller_class_name}`";
34-
throw new \Slim\Exception\HttpBadRequestException($request, $extra_log_message);
34+
35+
throw Utils::createSlimHttpExceptionWithLocalizedDescription(
36+
$container,
37+
SlimHttpExceptionClassNames::HttpBadRequestException,
38+
$request,
39+
$extra_log_message
40+
);
3541
}
3642

3743
if( !class_exists($controller_class_name) ) {
@@ -61,7 +67,12 @@ function sMVC_CreateController(
6167
//404 Not Found: Controller class not found.
6268
/** @psalm-suppress InvalidOperand */
6369
$extra_log_message = "`".__FILE__."` on line ".__LINE__.": Class `{$controller_class_name}` does not exist.";
64-
throw new \Slim\Exception\HttpNotFoundException($request, $extra_log_message);
70+
throw Utils::createSlimHttpExceptionWithLocalizedDescription(
71+
$container,
72+
SlimHttpExceptionClassNames::HttpNotFoundException,
73+
$request,
74+
$extra_log_message
75+
);
6576
}
6677
}
6778

@@ -73,7 +84,13 @@ function sMVC_CreateController(
7384
"`".__FILE__."` on line ".__LINE__
7485
. sprintf(': `%s` could not be mapped to a valid controller.', $request->getUri()->__toString());
7586

76-
throw new \Slim\Exception\HttpBadRequestException($request, $extra_log_message);
87+
//throw new \Slim\Exception\HttpBadRequestException($request, $extra_log_message);
88+
throw Utils::createSlimHttpExceptionWithLocalizedDescription(
89+
$container,
90+
SlimHttpExceptionClassNames::HttpBadRequestException,
91+
$request,
92+
$extra_log_message
93+
);
7794
}
7895

7996
//Create the controller object

0 commit comments

Comments
 (0)