Skip to content

Commit

Permalink
refactor: use specific exception
Browse files Browse the repository at this point in the history
  • Loading branch information
m90 committed Jan 15, 2024
1 parent 8ada66b commit fcfcf70
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
35 changes: 26 additions & 9 deletions dist-persist/wbstack/src/Info/GlobalSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@

namespace WBStack\Info;

class GlobalSetException extends \Exception {
public int $httpStatusCode;

public function __construct($msg, $httpStatusCode) {
parent::__construct($msg);
$this->$httpStatusCode = $httpStatusCode;
}
}

/**
* A class for setting a global holding a WBStackInfo object.
* This includes lookup of the data for the global from cache or API.
*/

class GlobalSet {

/**
Expand All @@ -24,9 +34,10 @@ public static function forDomain($requestDomain) {

$info = self::getCachedOrFreshInfo($requestDomain);

if($info === null) {
$notFound = new \Exception("No wiki was found for domain $requestDomain", 404);
throw $notFound;
if ($info === null) {
throw new GlobalSetException(
"No wiki was found for domain $requestDomain", 404,
);
}

self::setGlobal($info);
Expand Down Expand Up @@ -71,7 +82,14 @@ private static function getCachedOrFreshInfo( $requestDomain ) {
// TODO create an APC lock saying this proc is going to get fresh data?
// TODO in reality all of this needs to change...

$info = self::getInfoFromApi( $requestDomain );
try {
$info = self::getInfoFromApi( $requestDomain );
} catch (GlobalSetException $ex) {
if ($ex->httpStatusCode !== 404) {
throw $ex;
}
$info = null;
}

// Cache positive results for 10 seconds, negative for 2
$ttl = $info ? 10 : 2;
Expand Down Expand Up @@ -124,12 +142,11 @@ private static function getInfoFromApi( $requestDomain ) {
$response = curl_exec($client);
$responseCode = intval(curl_getinfo($client, CURLINFO_RESPONSE_CODE));

if ($responseCode === 404) {
return null;
}

if ($responseCode > 299) {
throw new \Exception("Unexpected status code $responseCode from Platform API for domain $requestDomain.", $responseCode);
throw new GlobalSetException(
"Unexpected status code $responseCode from Platform API for domain $requestDomain.",
$responseCode,
);
}

$info = WBStackInfo::newFromJsonString($response, $requestDomain);
Expand Down
10 changes: 7 additions & 3 deletions dist-persist/wbstack/src/Shim/Web.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
// Try and get the wiki info or fail
try {
\WBStack\Info\GlobalSet::forDomain($_SERVER['SERVER_NAME']);
} catch (Exception $ex) {
http_response_code($ex->getCode());
} catch (\WBStack\Info\GlobalSetException $ex) {
http_response_code($ex->httpStatusCode);
echo "You have requested the domain: " . $_SERVER['SERVER_NAME'] . ". But that wiki can not currently be loaded.\n";
echo "It may never have existed, it might now be deleted, or there was a server error.\n";
if ($ex->httpStatusCode === 404) {
echo "It may never have existed, it might now be deleted, or there was a server error.\n";
} else {
echo "There was a server error in the platform API.\n";
}
die(1);
}

Expand Down

0 comments on commit fcfcf70

Please sign in to comment.