mirror of
https://github.com/standardebooks/web.git
synced 2025-07-05 22:30:30 -04:00
Simplify exceptions in HttpInput
This commit is contained in:
parent
3ff13397f0
commit
0a684facee
3 changed files with 6 additions and 17 deletions
|
@ -1,5 +0,0 @@
|
||||||
<?
|
|
||||||
namespace Exceptions;
|
|
||||||
|
|
||||||
class InvalidHttpMethodException extends AppException{
|
|
||||||
}
|
|
|
@ -18,7 +18,7 @@ class HttpInput{
|
||||||
$filename = mb_strtolower($httpMethod->value) . '.php';
|
$filename = mb_strtolower($httpMethod->value) . '.php';
|
||||||
|
|
||||||
if(!file_exists($filename)){
|
if(!file_exists($filename)){
|
||||||
throw new Exceptions\InvalidHttpMethodException();
|
throw new Exceptions\HttpMethodNotAllowedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
if($httpMethod == Enums\HttpMethod::Post){
|
if($httpMethod == Enums\HttpMethod::Post){
|
||||||
|
@ -30,7 +30,7 @@ class HttpInput{
|
||||||
|
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
catch(Exceptions\InvalidHttpMethodException | Exceptions\HttpMethodNotAllowedException){
|
catch(Exceptions\HttpMethodNotAllowedException){
|
||||||
$filenames = glob('{delete,get,patch,post,put}.php', GLOB_BRACE);
|
$filenames = glob('{delete,get,patch,post,put}.php', GLOB_BRACE);
|
||||||
|
|
||||||
if(sizeof($filenames) > 0){
|
if(sizeof($filenames) > 0){
|
||||||
|
@ -47,8 +47,7 @@ class HttpInput{
|
||||||
*
|
*
|
||||||
* @param ?array<Enums\HttpMethod> $allowedHttpMethods An array containing a list of allowed HTTP methods, or null if any valid HTTP method is allowed.
|
* @param ?array<Enums\HttpMethod> $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.
|
* @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 not allowed, and `$throwException` is `true`.
|
||||||
* @throws Exceptions\HttpMethodNotAllowedException If the HTTP method is recognized but not allowed, and `$throwException` is `true`.
|
|
||||||
*/
|
*/
|
||||||
public static function ValidateRequestMethod(?array $allowedHttpMethods = null, bool $throwException = false): Enums\HttpMethod{
|
public static function ValidateRequestMethod(?array $allowedHttpMethods = null, bool $throwException = false): Enums\HttpMethod{
|
||||||
try{
|
try{
|
||||||
|
@ -66,14 +65,9 @@ class HttpInput{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch(\ValueError | Exceptions\HttpMethodNotAllowedException $ex){
|
catch(\ValueError | Exceptions\HttpMethodNotAllowedException){
|
||||||
if($throwException){
|
if($throwException){
|
||||||
if($ex instanceof \ValueError){
|
throw new Exceptions\HttpMethodNotAllowedException();
|
||||||
throw new Exceptions\InvalidHttpMethodException();
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
throw $ex;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
if($allowedHttpMethods !== null){
|
if($allowedHttpMethods !== null){
|
||||||
|
|
|
@ -97,7 +97,7 @@ try{
|
||||||
catch(Exceptions\LoginRequiredException){
|
catch(Exceptions\LoginRequiredException){
|
||||||
Template::RedirectToLogin();
|
Template::RedirectToLogin();
|
||||||
}
|
}
|
||||||
catch(Exceptions\InvalidPermissionsException | Exceptions\InvalidHttpMethodException | Exceptions\HttpMethodNotAllowedException){
|
catch(Exceptions\InvalidPermissionsException | Exceptions\HttpMethodNotAllowedException){
|
||||||
Template::ExitWithCode(Enums\HttpCode::Forbidden);
|
Template::ExitWithCode(Enums\HttpCode::Forbidden);
|
||||||
}
|
}
|
||||||
catch(Exceptions\InvalidEbookException | Exceptions\EbookPlaceholderExistsException | Exceptions\InvalidProjectException $ex){
|
catch(Exceptions\InvalidEbookException | Exceptions\EbookPlaceholderExistsException | Exceptions\InvalidProjectException $ex){
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue