diff --git a/dist-persist/wbstack/src/Info/GlobalSet.php b/dist-persist/wbstack/src/Info/GlobalSet.php index 491b4cf52..cd749d580 100644 --- a/dist-persist/wbstack/src/Info/GlobalSet.php +++ b/dist-persist/wbstack/src/Info/GlobalSet.php @@ -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); diff --git a/dist-persist/wbstack/src/Shim/Web.php b/dist-persist/wbstack/src/Shim/Web.php index 65000f177..f11c557d4 100644 --- a/dist-persist/wbstack/src/Shim/Web.php +++ b/dist-persist/wbstack/src/Shim/Web.php @@ -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); } diff --git a/dist/wbstack/src/Info/GlobalSet.php b/dist/wbstack/src/Info/GlobalSet.php index 491b4cf52..cd749d580 100644 --- a/dist/wbstack/src/Info/GlobalSet.php +++ b/dist/wbstack/src/Info/GlobalSet.php @@ -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); diff --git a/dist/wbstack/src/Shim/Web.php b/dist/wbstack/src/Shim/Web.php index 65000f177..f11c557d4 100644 --- a/dist/wbstack/src/Shim/Web.php +++ b/dist/wbstack/src/Shim/Web.php @@ -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); }