Skip to content

Commit

Permalink
Fix misleading error when viewing details for an album not found from…
Browse files Browse the repository at this point in the history
… Last.fm

In this particular case, Last.fm returns the status code 404. This was
incorrectly interpreted as a sign of broken Last.fm connection and the error
message suggested that maybe the API key is incorrect. With this commit, the
status code 404 is give a special treatment so that it is not considered a
connectivity issue.
  • Loading branch information
paulijar committed Aug 28, 2021
1 parent f746271 commit 591be8e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
- Last.fm details view not showing the tag correctly if the track/album/artist has only one tag
- Ampache client [AmpacheAlbumPlayer](https://play.google.com/store/apps/details?id=com.stemaker.ampachealbumplayer) being incompatible
- Continuing playback from the same offset when moving from Files to Music (broken since 1.0.0)
- Misleading error message shown when viewing details for an album not found from Last.fm

## 1.2.1 - 2021-06-27
### Added
Expand Down
19 changes: 17 additions & 2 deletions lib/Utility/LastfmService.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,16 +201,31 @@ private function getInfoFromLastFm($args) {
// ... and form the final query string
$queryString = '?' . \implode('&', $args);

$info = \file_get_contents(self::LASTFM_URL . $queryString);
list($info, $statusCode, $msg) = self::fetchUrl(self::LASTFM_URL . $queryString);
$statusCode = (int)$statusCode;

if ($info === false) {
$info = ['connection_ok' => false];
// When an album is not found, Last.fm returns 404 but that is not a sign of broken connection.
// Interestingly, not finding an artist is still responded with the code 200.
$info = ['connection_ok' => ($statusCode === 404)];
} else {
$info = \json_decode($info, true);
$info['connection_ok'] = true;
}
$info['status_code'] = $statusCode;
$info['status_msg'] = $msg;
$info['api_key_set'] = true;
return $info;
}
}

private static function fetchUrl($url) : array {
$content = \file_get_contents($url);

// It's some PHP magic that calling file_get_contents creates and populates
// also a local variable array $http_response_header.
list($version, $status_code, $msg) = explode(' ', $http_response_header[0], 3);

return [$content, $status_code, $msg];
}
}

0 comments on commit 591be8e

Please sign in to comment.