Skip to content

Commit

Permalink
Make update check more resilient to API being down (#2987)
Browse files Browse the repository at this point in the history
  • Loading branch information
tadhgboyle authored Aug 7, 2022
1 parent 46fd4b4 commit 3726a17
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion core/classes/Core/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public static function updateCheck() {
return $update_check_response->getError();
}

$update_check = new UpdateCheck($update_check_response->json(true));
$update_check = new UpdateCheck($update_check_response);
if ($update_check->hasError()) {
return $update_check->getErrorMessage();
}
Expand Down
16 changes: 11 additions & 5 deletions core/classes/DTO/UpdateCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,24 @@
*/
class UpdateCheck {

private array $_response;
private ?string $_raw_response;
private ?array $_response;

public function __construct(array $response) {
$this->_response = $response;
public function __construct(HttpClient $update_check_response) {
$this->_raw_response = $update_check_response->contents();
$this->_response = json_decode($this->_raw_response, true);
}

public function hasError(): bool {
return !count($this->_response) || $this->_response['error'];
return $this->_response === null || !count($this->_response) || $this->_response['error'];
}

public function getErrorMessage(): string {
return $this->_response['message'] ?? 'Invalid response from server: ' . json_encode($this->_response);
if (isset($this->_response['message'])) {
return 'Error from server: ' . $this->_response['message'];
}

return 'Invalid response from server: ' . $this->_raw_response;
}

public function updateAvailable(): bool {
Expand Down

0 comments on commit 3726a17

Please sign in to comment.