From 0a684faceedd2f2689e2880dd18cebf5cddeaf9f Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Mon, 24 Feb 2025 14:42:49 -0600 Subject: [PATCH] Simplify exceptions in HttpInput --- lib/Exceptions/InvalidHttpMethodException.php | 5 ----- lib/HttpInput.php | 16 +++++----------- www/ebook-placeholders/post.php | 2 +- 3 files changed, 6 insertions(+), 17 deletions(-) delete mode 100644 lib/Exceptions/InvalidHttpMethodException.php diff --git a/lib/Exceptions/InvalidHttpMethodException.php b/lib/Exceptions/InvalidHttpMethodException.php deleted file mode 100644 index d28dff0a..00000000 --- a/lib/Exceptions/InvalidHttpMethodException.php +++ /dev/null @@ -1,5 +0,0 @@ -value) . '.php'; if(!file_exists($filename)){ - throw new Exceptions\InvalidHttpMethodException(); + throw new Exceptions\HttpMethodNotAllowedException(); } if($httpMethod == Enums\HttpMethod::Post){ @@ -30,7 +30,7 @@ class HttpInput{ exit(); } - catch(Exceptions\InvalidHttpMethodException | Exceptions\HttpMethodNotAllowedException){ + catch(Exceptions\HttpMethodNotAllowedException){ $filenames = glob('{delete,get,patch,post,put}.php', GLOB_BRACE); if(sizeof($filenames) > 0){ @@ -47,8 +47,7 @@ class HttpInput{ * * @param ?array $allowedHttpMethods An array containing a list of allowed HTTP methods, or null if any valid HTTP method is allowed. * @param bool $throwException If the request HTTP method isn't allowed, then throw an exception; otherwise, output HTTP 405 and exit the script immediately. - * @throws Exceptions\InvalidHttpMethodException If the HTTP method is not recognized, and `$throwException` is `true`. - * @throws Exceptions\HttpMethodNotAllowedException If the HTTP method is recognized but not allowed, and `$throwException` is `true`. + * @throws Exceptions\HttpMethodNotAllowedException If the HTTP method is not allowed, and `$throwException` is `true`. */ public static function ValidateRequestMethod(?array $allowedHttpMethods = null, bool $throwException = false): Enums\HttpMethod{ try{ @@ -66,14 +65,9 @@ class HttpInput{ } } } - catch(\ValueError | Exceptions\HttpMethodNotAllowedException $ex){ + catch(\ValueError | Exceptions\HttpMethodNotAllowedException){ if($throwException){ - if($ex instanceof \ValueError){ - throw new Exceptions\InvalidHttpMethodException(); - } - else{ - throw $ex; - } + throw new Exceptions\HttpMethodNotAllowedException(); } else{ if($allowedHttpMethods !== null){ diff --git a/www/ebook-placeholders/post.php b/www/ebook-placeholders/post.php index a6f5a18a..23bd0737 100644 --- a/www/ebook-placeholders/post.php +++ b/www/ebook-placeholders/post.php @@ -97,7 +97,7 @@ try{ catch(Exceptions\LoginRequiredException){ Template::RedirectToLogin(); } -catch(Exceptions\InvalidPermissionsException | Exceptions\InvalidHttpMethodException | Exceptions\HttpMethodNotAllowedException){ +catch(Exceptions\InvalidPermissionsException | Exceptions\HttpMethodNotAllowedException){ Template::ExitWithCode(Enums\HttpCode::Forbidden); } catch(Exceptions\InvalidEbookException | Exceptions\EbookPlaceholderExistsException | Exceptions\InvalidProjectException $ex){