From 591be8e97e06797a3daa4df2d1380ad1ebe70262 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pauli=20J=C3=A4rvinen?= Date: Sat, 28 Aug 2021 16:58:38 +0300 Subject: [PATCH] Fix misleading error when viewing details for an album not found from 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. --- CHANGELOG.md | 1 + lib/Utility/LastfmService.php | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bd137c517..76873b567 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/Utility/LastfmService.php b/lib/Utility/LastfmService.php index 255d52fa6..148291561 100644 --- a/lib/Utility/LastfmService.php +++ b/lib/Utility/LastfmService.php @@ -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]; + } }