Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: propagate status code from platform api when routing wiki requests #410

Merged
merged 7 commits into from
Jan 16, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: use specific exception
m90 committed Jan 15, 2024
commit b7666ab6ae816c8236236eb0291684b2ea2579bf
28 changes: 19 additions & 9 deletions dist-persist/wbstack/src/Info/GlobalSet.php
Original file line number Diff line number Diff line change
@@ -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 {

/**
@@ -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);
@@ -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;
@@ -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);
11 changes: 8 additions & 3 deletions dist-persist/wbstack/src/Shim/Web.php
Original file line number Diff line number Diff line change
@@ -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);
}

28 changes: 19 additions & 9 deletions dist/wbstack/src/Info/GlobalSet.php
Original file line number Diff line number Diff line change
@@ -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 {

/**
@@ -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);
@@ -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;
@@ -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);
11 changes: 8 additions & 3 deletions dist/wbstack/src/Shim/Web.php
Original file line number Diff line number Diff line change
@@ -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);
}