Use HTTP code enums instead of ints

This commit is contained in:
Alex Cabal 2024-11-20 14:52:05 -06:00
parent 3f822b85c3
commit 3050ab7219
28 changed files with 56 additions and 56 deletions

View file

@ -34,13 +34,13 @@ class Template{
}
public static function Emit403(): void{
http_response_code(403);
http_response_code(Enums\HttpCode::Forbidden->value);
include(WEB_ROOT . '/403.php');
exit();
}
public static function Emit404(): void{
http_response_code(404);
http_response_code(Enums\HttpCode::NotFound->value);
include(WEB_ROOT . '/404.php');
exit();
}

View file

@ -23,7 +23,7 @@ try{
// We got here because an artwork update had errors and the user has to try again.
if($exception){
http_response_code(422);
http_response_code(Enums\HttpCode::UnprocessableContent->value);
session_unset();
}
}

View file

@ -47,7 +47,7 @@ try{
// We got here because an artwork PATCH operation had errors and the user has to try again.
if($exception){
http_response_code(422);
http_response_code(Enums\HttpCode::UnprocessableContent->value);
// Before we overwrite the original artwork with our new one, restore the old status, because if the new status is 'approved' then it will hide the status form entirely, which will be confusing.
$oldStatus = $artwork->Status;

View file

@ -20,14 +20,14 @@ try{
// We got here because an artwork was successfully submitted.
if($isCreated){
http_response_code(201);
http_response_code(Enums\HttpCode::Created->value);
$artwork = null;
session_unset();
}
// We got here because an artwork submission had errors and the user has to try again.
if($exception){
http_response_code(422);
http_response_code(Enums\HttpCode::UnprocessableContent->value);
session_unset();
}

View file

@ -40,7 +40,7 @@ try{
$_SESSION['artwork'] = $artwork;
$_SESSION['is-created'] = true;
http_response_code(303);
http_response_code(Enums\HttpCode::SeeOther->value);
header('Location: /artworks/new');
}
@ -80,7 +80,7 @@ try{
$_SESSION['artwork'] = $artwork;
$_SESSION['is-saved'] = true;
http_response_code(303);
http_response_code(Enums\HttpCode::SeeOther->value);
header('Location: ' . $artwork->Url);
}
@ -121,7 +121,7 @@ try{
$_SESSION['artwork'] = $artwork;
$_SESSION['is-saved'] = true;
http_response_code(303);
http_response_code(Enums\HttpCode::SeeOther->value);
header('Location: ' . $artwork->Url);
}
}
@ -143,6 +143,6 @@ catch(Exceptions\InvalidArtworkException | Exceptions\InvalidArtworkTagException
$_SESSION['artwork'] = $artwork;
$_SESSION['exception'] = $ex;
http_response_code(303);
http_response_code(Enums\HttpCode::SeeOther->value);
header('Location: ' . $exceptionRedirectUrl);
}

View file

@ -46,7 +46,7 @@ catch(Exceptions\LoginRequiredException){
}
}
catch(Exceptions\InvalidPermissionsException){
http_response_code(403);
http_response_code(Enums\HttpCode::Forbidden->value);
}
catch(Exceptions\InvalidFileException){
Template::Emit404();

View file

@ -56,7 +56,7 @@ catch(Exceptions\EbookNotFoundException){
$ebook = Ebook::GetByIdentifierStartingWith($identifier);
// Found, redirect.
http_response_code(301);
http_response_code(Enums\HttpCode::MovedPermanently->value);
header('Location: ' . $ebook->Url);
exit();
}

View file

@ -99,7 +99,7 @@ catch(Exceptions\PageOutOfBoundsException){
}
catch(Exceptions\AppException $ex){
// Something very unexpected happened, log and emit 500.
http_response_code(500); // Internal server error.
http_response_code(Enums\HttpCode::InternalServerError->value); // Internal server error.
Log::WriteErrorLogEntry($ex);
exit();
}

View file

@ -11,7 +11,7 @@ try{
}
}
catch(\Exception){
http_response_code(500);
http_response_code(Enums\HttpCode::InternalServerError->value);
include(WEB_ROOT . '/404.php');
exit();
}

View file

@ -98,10 +98,10 @@ try{
}
catch(Exceptions\LoginRequiredException){
header('WWW-Authenticate: Basic realm="Enter your Patrons Circle email address and leave the password empty."');
http_response_code(401);
http_response_code(Enums\HttpCode::Unauthorized->value);
}
catch(Exceptions\InvalidPermissionsException){
http_response_code(403);
http_response_code(Enums\HttpCode::Forbidden->value);
}
catch(Exceptions\InvalidFileException){
Template::Emit404();

View file

@ -11,7 +11,7 @@ try{
}
}
catch(\Exception){
http_response_code(500);
http_response_code(Enums\HttpCode::InternalServerError->value);
exit();
}
print("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<?xml-stylesheet href=\"". SITE_URL . "/feeds/opds/style\" type=\"text/xsl\"?>\n");

View file

@ -11,7 +11,7 @@ try{
}
}
catch(\Exception){
http_response_code(500);
http_response_code(Enums\HttpCode::InternalServerError->value);
include(WEB_ROOT . '/404.php');
exit();
}

View file

@ -22,5 +22,5 @@ if($url != ''){
$url = '/' . $url;
}
http_response_code(302);
http_response_code(Enums\HttpCode::Found->value);
header('Location: /manual/' . $currentManual . $url);

View file

@ -11,7 +11,7 @@ try{
$_SESSION['subscription-confirmed'] = $subscription->UserId;
}
http_response_code(303);
http_response_code(Enums\HttpCode::SeeOther->value);
header('Location: ' . $subscription->Url);
}
catch(Exceptions\NewsletterSubscriptionNotFoundException){

View file

@ -17,7 +17,7 @@ catch(Exceptions\NewsletterSubscriptionNotFoundException){
Template::Emit404();
}
else{
http_response_code(404);
http_response_code(Enums\HttpCode::NotFound->value);
exit();
}
}

View file

@ -34,7 +34,7 @@ try{
if($created){
// HTTP 201 Created
http_response_code(201);
http_response_code(Enums\HttpCode::Created->value);
}
}
catch(Exceptions\AppException){

View file

@ -9,7 +9,7 @@ $subscription = $_SESSION['subscription'] ?? new NewsletterSubscription();
$exception = $_SESSION['exception'] ?? null;
if($exception){
http_response_code(422);
http_response_code(Enums\HttpCode::UnprocessableContent->value);
session_unset();
}

View file

@ -14,7 +14,7 @@ try{
if(HttpInput::Str(POST, 'automationtest')){
// A bot filled out this form field, which should always be empty. Pretend like we succeeded.
if($requestType == Enums\HttpRequestType::Web){
http_response_code(303);
http_response_code(Enums\HttpCode::SeeOther->value);
$uuid = Uuid::uuid4();
$subscription->User = new User();
$subscription->User->Uuid = $uuid->toString();
@ -23,7 +23,7 @@ try{
}
else{
// Access via Enums\HttpRequestType::Rest api; 201 CREATED with location
http_response_code(201);
http_response_code(Enums\HttpCode::Created->value);
header('Location: /newsletter/subscriptions/success');
}
@ -43,13 +43,13 @@ try{
session_unset();
if($requestType == Enums\HttpRequestType::Web){
http_response_code(303);
http_response_code(Enums\HttpCode::SeeOther->value);
$_SESSION['is-subscription-created'] = $subscription->UserId;
header('Location: /newsletter/subscriptions/success');
}
else{
// Access via Enums\HttpRequestType::Rest api; 201 CREATED with location
http_response_code(201);
http_response_code(Enums\HttpCode::Created->value);
header('Location: /newsletter/subscriptions/success');
}
}
@ -62,7 +62,7 @@ catch(Exceptions\NewsletterSubscriptionExistsException){
$subscription->IsConfirmed = $existingSubscription->IsConfirmed;
$subscription->Save();
http_response_code(303);
http_response_code(Enums\HttpCode::SeeOther->value);
if(!$subscription->IsConfirmed){
// Don't re-send the email after all, to prevent spam
@ -77,7 +77,7 @@ catch(Exceptions\NewsletterSubscriptionExistsException){
}
else{
// Access via Enums\HttpRequestType::Rest api; 409 CONFLICT
http_response_code(409);
http_response_code(Enums\HttpCode::Conflict->value);
}
}
catch(Exceptions\InvalidNewsletterSubscription $ex){
@ -86,11 +86,11 @@ catch(Exceptions\InvalidNewsletterSubscription $ex){
$_SESSION['exception'] = $ex;
// Access via form; 303 redirect to the form, which will emit a 422 Unprocessable Entity
http_response_code(303);
http_response_code(Enums\HttpCode::SeeOther->value);
header('Location: /newsletter/subscriptions/new');
}
else{
// Access via Enums\HttpRequestType::Rest api; 422 Unprocessable Entity
http_response_code(422);
http_response_code(Enums\HttpCode::UnprocessableContent->value);
}
}

View file

@ -16,7 +16,7 @@ if(isset($_SESSION['is-subscription-created'])){
if($created){
// HTTP 201 Created
http_response_code(201);
http_response_code(Enums\HttpCode::Created->value);
}
?><?= Template::Header(['title' => 'Your subscription to the Standard Ebooks newsletter', 'highlight' => 'newsletter', 'description' => 'Your subscription to the Standard Ebooks newsletter.']) ?>

View file

@ -11,7 +11,7 @@ try{
if(isset($_SESSION['is-vote-created']) && $_SESSION['is-vote-created'] == $vote->UserId){
$created = true;
http_response_code(201);
http_response_code(Enums\HttpCode::Created->value);
session_unset();
}
}

View file

@ -36,7 +36,7 @@ try{
}
if($exception){
http_response_code(422);
http_response_code(Enums\HttpCode::UnprocessableContent->value);
session_unset();
}
}

View file

@ -18,12 +18,12 @@ try{
if($requestType == Enums\HttpRequestType::Web){
$_SESSION['is-vote-created'] = $vote->UserId;
http_response_code(303);
http_response_code(Enums\HttpCode::SeeOther->value);
header('Location: ' . $vote->Url);
}
else{
// Access via Enums\HttpRequestType::Rest api; 201 CREATED with location
http_response_code(201);
http_response_code(Enums\HttpCode::Created->value);
header('Location: ' . $vote->Url);
}
}
@ -33,11 +33,11 @@ catch(Exceptions\InvalidPollVoteException $ex){
$_SESSION['exception'] = $ex;
// Access via form; 303 redirect to the form, which will emit a 422 Unprocessable Entity
http_response_code(303);
http_response_code(Enums\HttpCode::SeeOther->value);
header('Location: /polls/' . (HttpInput::Str(GET, 'pollurlname') ?? '') . '/votes/new');
}
else{
// Access via Enums\HttpRequestType::Rest api; 422 Unprocessable Entity
http_response_code(422);
http_response_code(Enums\HttpCode::UnprocessableContent->value);
}
}

View file

@ -14,18 +14,18 @@ $redirect = HttpInput::Str(SESSION, 'redirect') ?? HttpInput::Str(GET, 'redirect
$exception = $_SESSION['exception'] ?? null;
$passwordRequired = false;
http_response_code(401);
http_response_code(Enums\HttpCode::Unauthorized->value);
if($exception){
if($exception instanceof Exceptions\PasswordRequiredException){
// This login requires a password to proceed.
// Prompt the user for a password.
http_response_code(401);
http_response_code(Enums\HttpCode::Unauthorized->value);
$passwordRequired = true;
$exception = null; // Clear the exception so we don't show an error
}
else{
http_response_code(422);
http_response_code(Enums\HttpCode::UnprocessableContent->value);
}
session_unset();
}

View file

@ -22,12 +22,12 @@ try{
session_unset();
if($requestType == Enums\HttpRequestType::Web){
http_response_code(303);
http_response_code(Enums\HttpCode::SeeOther->value);
header('Location: ' . $redirect);
}
else{
// Access via Enums\HttpRequestType::Rest api; 201 CREATED with location
http_response_code(201);
http_response_code(Enums\HttpCode::Created->value);
header('Location: ' . $session->Url);
}
}
@ -38,11 +38,11 @@ catch(Exceptions\InvalidLoginException | Exceptions\PasswordRequiredException $e
$_SESSION['exception'] = $ex;
// Access via form; 303 redirect to the form, which will emit a 422 Unprocessable Entity
http_response_code(303);
http_response_code(Enums\HttpCode::SeeOther->value);
header('Location: /sessions/new');
}
else{
// Access via Enums\HttpRequestType::Rest api; 422 Unprocessable Entity
http_response_code(422);
http_response_code(Enums\HttpCode::UnprocessableContent->value);
}
}

View file

@ -23,7 +23,7 @@ if($colorScheme !== null){
}
// HTTP 303, See other
http_response_code(303);
http_response_code(Enums\HttpCode::SeeOther->value);
$redirect = $_SERVER['HTTP_REFERER'] ?? '/';
header('Location: ' . $redirect);

View file

@ -131,11 +131,11 @@ try{
}
// "Success, no content"
http_response_code(204);
http_response_code(Enums\HttpCode::NoContent->value);
}
catch(Exceptions\InvalidCredentialsException){
// "Forbidden"
http_response_code(403);
http_response_code(Enums\HttpCode::Forbidden->value);
}
catch(Exceptions\WebhookException $ex){
// Uh oh, something went wrong!
@ -147,13 +147,13 @@ catch(Exceptions\WebhookException $ex){
print($ex->getMessage());
// "Client error"
http_response_code(400);
http_response_code(Enums\HttpCode::BadRequest->value);
}
catch(Exceptions\NoopException){
// We arrive here because a special case required us to take no action for the request, but execution also had to be interrupted.
// For example, we received a request for a known repo for which we must ignore requests.
// "Success, no content"
http_response_code(204);
http_response_code(Enums\HttpCode::NoContent->value);
}
?>

View file

@ -74,12 +74,12 @@ try{
$log->Write('Event processed.');
// "Success, no content"
http_response_code(204);
http_response_code(Enums\HttpCode::NoContent->value);
}
catch(Exceptions\InvalidCredentialsException){
// "Forbidden"
$log->Write('Invalid key: ' . ($_SERVER['HTTP_X_SE_KEY'] ?? ''));
http_response_code(403);
http_response_code(Enums\HttpCode::Forbidden->value);
}
catch(Exceptions\WebhookException $ex){
// Uh oh, something went wrong!
@ -91,5 +91,5 @@ catch(Exceptions\WebhookException $ex){
print($ex->getMessage());
// "Client error"
http_response_code(400);
http_response_code(Enums\HttpCode::BadRequest->value);
}

View file

@ -51,12 +51,12 @@ try{
$log->Write('Event processed.');
// "Success, no content"
http_response_code(204);
http_response_code(Enums\HttpCode::NoContent->value);
}
catch(Exceptions\InvalidCredentialsException){
// "Forbidden"
$log->Write('Couldn\'t validate POST data.');
http_response_code(403);
http_response_code(Enums\HttpCode::Forbidden->value);
}
catch(Exceptions\WebhookException $ex){
// Uh oh, something went wrong!
@ -68,5 +68,5 @@ catch(Exceptions\WebhookException $ex){
print($ex->getMessage());
// "Client error"
http_response_code(400);
http_response_code(Enums\HttpCode::BadRequest->value);
}