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 b7666ab
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 24 deletions.
28 changes: 19 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,13 @@

namespace WBStack\Info;

class GlobalSetException extends \Exception {}

/**
* 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 +27,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 +75,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->getCode() !== 404) {
throw $ex;
}
$info = null;
}

// Cache positive results for 10 seconds, negative for 2
$ttl = $info ? 10 : 2;
Expand Down Expand Up @@ -124,12 +135,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
11 changes: 8 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,15 @@
// Try and get the wiki info or fail
try {
\WBStack\Info\GlobalSet::forDomain($_SERVER['SERVER_NAME']);
} catch (Exception $ex) {
} catch (\WBStack\Info\GlobalSetException $ex) {
http_response_code($ex->getCode());
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";
echo "You have requested the domain: " . $_SERVER['SERVER_NAME'] . ". But that wiki can not currently be loaded.".PHP_EOL;
if ($ex->getCode() === 404) {
echo "It may never have existed or it might now be deleted.".PHP_EOL;
} else {
echo "There was a server error in the platform API.".PHP_EOL;
}
echo $ex->getMessage();
die(1);
}

Expand Down
28 changes: 19 additions & 9 deletions dist/wbstack/src/Info/GlobalSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace WBStack\Info;

class GlobalSetException extends \Exception {}

/**
* 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 +27,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 +75,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->getCode() !== 404) {
throw $ex;
}
$info = null;
}

// Cache positive results for 10 seconds, negative for 2
$ttl = $info ? 10 : 2;
Expand Down Expand Up @@ -124,12 +135,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
11 changes: 8 additions & 3 deletions dist/wbstack/src/Shim/Web.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
// Try and get the wiki info or fail
try {
\WBStack\Info\GlobalSet::forDomain($_SERVER['SERVER_NAME']);
} catch (Exception $ex) {
} catch (\WBStack\Info\GlobalSetException $ex) {
http_response_code($ex->getCode());
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";
echo "You have requested the domain: " . $_SERVER['SERVER_NAME'] . ". But that wiki can not currently be loaded.".PHP_EOL;
if ($ex->getCode() === 404) {
echo "It may never have existed or it might now be deleted.".PHP_EOL;
} else {
echo "There was a server error in the platform API.".PHP_EOL;
}
echo $ex->getMessage();
die(1);
}

Expand Down

0 comments on commit b7666ab

Please sign in to comment.