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
Next Next commit
fix: propagate status code from platform api when routing wiki requests
m90 committed Jan 11, 2024
commit 8ada66b0e15f8618d16562108427713f4e9a0f72
32 changes: 18 additions & 14 deletions dist-persist/wbstack/src/Info/GlobalSet.php
Original file line number Diff line number Diff line change
@@ -10,26 +10,26 @@ class GlobalSet {

/**
* @param string $requestDomain A request domain, or 'maint' Example: 'addshore-alpha.wiki.opencura.com'
* @return bool was the global successfully set?
* @return void
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this makes sense and probably propagating the precise nature of the failure with an Exception.

In any case this probably makes sense and is then super clear that this is really a side effect only function.

Naturally trying to write this in a clean way is pretty hard since the desired behaviour is charging around a setting a load of globals

*/
public static function forDomain( $requestDomain ) {
public static function forDomain($requestDomain) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the linting here looks "funny" because MediaWiki uses a very non-php standard code style. No objection to this being changed here since we don't lint this anyway but perhaps in future we should add linting to match the MediaWiki style to this weird codebase in dist-persist

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean if we want a code style, then it should be applied everywhere. I just tried to make it consistent per function. Which is probably futile ...

// Normalize the domain by setting to lowercase
$requestDomain = strtolower($requestDomain);

// If the domain is 'maint' then set the maint settings
if($requestDomain === 'maint') {
if ($requestDomain === 'maint') {
self::forMaint();
return true;
return;
}

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

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

self::setGlobal( $info );
return true;
self::setGlobal($info);
}

/**
@@ -120,16 +120,20 @@ private static function getInfoFromApi( $requestDomain ) {
curl_setopt($client, CURLOPT_HTTPHEADER, $headers);
curl_setopt($client, CURLOPT_RETURNTRANSFER, true);
curl_setopt( $client, CURLOPT_USERAGENT, "WBStack - MediaWiki - WBStackInfo::getInfoFromApi" );

$response = curl_exec($client);
$responseCode = intval(curl_getinfo($client, CURLINFO_RESPONSE_CODE));

// TODO detect non 200 response here, and pass that out to the user as an error

$info = WBStackInfo::newFromJsonString($response, $requestDomain);
if (!$info) {
if ($responseCode === 404) {
return null;
}

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

$info = WBStackInfo::newFromJsonString($response, $requestDomain);
return $info;
}

}
}
6 changes: 4 additions & 2 deletions dist-persist/wbstack/src/Shim/Cli.php
Original file line number Diff line number Diff line change
@@ -15,7 +15,9 @@
}

// Try and get the wiki info (from env var) or fail with a message
if(!\WBStack\Info\GlobalSet::forDomain( getenv( 'WBS_DOMAIN' ) )) {
try {
\WBStack\Info\GlobalSet::forDomain( getenv( 'WBS_DOMAIN' ) );
} catch (Exception $ex) {
echo 'Failed to work for WBS_DOMAIN: ' . getenv( 'WBS_DOMAIN' );
die(1);
}
}
10 changes: 6 additions & 4 deletions dist-persist/wbstack/src/Shim/Web.php
Original file line number Diff line number Diff line change
@@ -2,11 +2,13 @@

require_once __DIR__ . '/../loadShim.php';

// Try and get the wiki info, for fail with a 404 web page
if(!\WBStack\Info\GlobalSet::forDomain( $_SERVER['SERVER_NAME'] ) ) {
http_response_code(404);
// Try and get the wiki info or fail
try {
\WBStack\Info\GlobalSet::forDomain($_SERVER['SERVER_NAME']);
} catch (Exception $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, or it might now be deleted.\n";
echo "It may never have existed, it might now be deleted, or there was a server error.\n";
die(1);
}

32 changes: 18 additions & 14 deletions dist/wbstack/src/Info/GlobalSet.php
Original file line number Diff line number Diff line change
@@ -10,26 +10,26 @@ class GlobalSet {

/**
* @param string $requestDomain A request domain, or 'maint' Example: 'addshore-alpha.wiki.opencura.com'
* @return bool was the global successfully set?
* @return void
*/
public static function forDomain( $requestDomain ) {
public static function forDomain($requestDomain) {
// Normalize the domain by setting to lowercase
$requestDomain = strtolower($requestDomain);

// If the domain is 'maint' then set the maint settings
if($requestDomain === 'maint') {
if ($requestDomain === 'maint') {
self::forMaint();
return true;
return;
}

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

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

self::setGlobal( $info );
return true;
self::setGlobal($info);
}

/**
@@ -120,16 +120,20 @@ private static function getInfoFromApi( $requestDomain ) {
curl_setopt($client, CURLOPT_HTTPHEADER, $headers);
curl_setopt($client, CURLOPT_RETURNTRANSFER, true);
curl_setopt( $client, CURLOPT_USERAGENT, "WBStack - MediaWiki - WBStackInfo::getInfoFromApi" );

$response = curl_exec($client);
$responseCode = intval(curl_getinfo($client, CURLINFO_RESPONSE_CODE));

// TODO detect non 200 response here, and pass that out to the user as an error

$info = WBStackInfo::newFromJsonString($response, $requestDomain);
if (!$info) {
if ($responseCode === 404) {
return null;
}

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

$info = WBStackInfo::newFromJsonString($response, $requestDomain);
return $info;
}

}
}
6 changes: 4 additions & 2 deletions dist/wbstack/src/Shim/Cli.php
Original file line number Diff line number Diff line change
@@ -15,7 +15,9 @@
}

// Try and get the wiki info (from env var) or fail with a message
if(!\WBStack\Info\GlobalSet::forDomain( getenv( 'WBS_DOMAIN' ) )) {
try {
\WBStack\Info\GlobalSet::forDomain( getenv( 'WBS_DOMAIN' ) );
} catch (Exception $ex) {
echo 'Failed to work for WBS_DOMAIN: ' . getenv( 'WBS_DOMAIN' );
die(1);
}
}
10 changes: 6 additions & 4 deletions dist/wbstack/src/Shim/Web.php
Original file line number Diff line number Diff line change
@@ -2,11 +2,13 @@

require_once __DIR__ . '/../loadShim.php';

// Try and get the wiki info, for fail with a 404 web page
if(!\WBStack\Info\GlobalSet::forDomain( $_SERVER['SERVER_NAME'] ) ) {
http_response_code(404);
// Try and get the wiki info or fail
try {
\WBStack\Info\GlobalSet::forDomain($_SERVER['SERVER_NAME']);
} catch (Exception $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, or it might now be deleted.\n";
echo "It may never have existed, it might now be deleted, or there was a server error.\n";
die(1);
}