Skip to content

Commit

Permalink
Tweak to exempt \Slim\Exception\HttpSpecializedException exceptions f…
Browse files Browse the repository at this point in the history
…rom being handled when an exception is thrown when an action method is called on the controller
  • Loading branch information
rotimi committed Feb 24, 2025
1 parent 3a5ec6b commit 6f02e29
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 28 deletions.
58 changes: 31 additions & 27 deletions src/MvcRouteHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ public function __invoke(

: $controller_obj->$action_method(...$params); // handle this route
// '/{controller}/{action}[/{parameters:.+}]'

// If we got this far, that means that the action method was successfully
// executed on the controller object.
if( is_string($actn_res) ) {

$resp = $pre_action_response;
// write the string into the response object as the response body
$resp->getBody()->write($actn_res);

} elseif ( $actn_res instanceof \Psr\Http\Message\ResponseInterface ) {

$resp = $actn_res; // the action returned a Response object
}

} catch (\ArgumentCountError $e) {

//400 Bad Request: Not enough arguments supplied in the uri to invoke the method above.
Expand All @@ -102,33 +116,23 @@ public function __invoke(

} catch (\Throwable $e) {

//500 Internal server error: An Exception or Error was thrown when trying to execute action method on the controller.
/** @psalm-suppress InvalidOperand */
$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());

/** @psalm-suppress PossiblyNullArgument */
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
// executed on the controller object.
if( is_string($actn_res) ) {

$resp = $pre_action_response;
// write the string into the response object as the response body
$resp->getBody()->write($actn_res);

} elseif ( $actn_res instanceof \Psr\Http\Message\ResponseInterface ) {

$resp = $actn_res; // the action returned a Response object
if(! $e instanceof \Slim\Exception\HttpSpecializedException) {

//500 Internal server error: An Exception or Error was thrown when trying to execute action method on the controller.
/** @psalm-suppress InvalidOperand */
$log_message =
"`".__FILE__."` on line ".__LINE__
. sprintf(': Error occured when calling `%s`(...) on an instance of `%s` for the uri `%s`.' . PHP_EOL . 'Error: `%s`', $action_method, $controller_obj::class, $req->getUri()->__toString(), $e->getMessage());

/** @psalm-suppress PossiblyNullArgument */
throw Utils::createSlimHttpExceptionWithLocalizedDescription(
$this->app->getContainer(),
SlimHttpExceptionClassNames::HttpInternalServerErrorException,
$req,
$log_message,
$e
);
}
}

return $controller_obj->postAction($resp);
Expand Down
1 change: 0 additions & 1 deletion src/functions/framework-helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ 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 Utils::createSlimHttpExceptionWithLocalizedDescription(
$container,
SlimHttpExceptionClassNames::HttpBadRequestException,
Expand Down

0 comments on commit 6f02e29

Please sign in to comment.