[GH-ISSUE #269] TypeError when API returns null on getMyCurrentTrack #198

Closed
opened 2026-02-27 19:26:33 +03:00 by kerem · 3 comments
Owner

Originally created by @joeygallegos on GitHub (Dec 14, 2023).
Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/269

I am writing an application that tracks what song I am currently listening to. When I stop playing music and exit the desktop app, I get the below TypeError exception because the API is returning a null response but the function is declared to only allow array|object as return types. Right now, when I wrap the code in a try/catch, it doesn't even run the caught exception. When playing music, I get no error.

Exception:

Type: TypeError
Code: 0
Message: SpotifyWebAPI\SpotifyWebAPI::getMyCurrentTrack(): Return value must be of type object|array, null returned
File: D:\Dropbox\Web Work\joey\vendor\jwilsson\spotify-web-api-php\src\SpotifyWebAPI.php
Line: 1149

I have my code setup like this:

$options = [
	'auto_refresh' => true,
	'return_assoc' => true
];
$api = new SpotifyWebAPI\SpotifyWebAPI($options, $session);
try {
	$spotify = $this->container->get('spotify');
	$currentTrack = $spotify['api']->getMyCurrentTrack();
	// TODO: Exception when no music is playing. Null value comes forward, but function throws exception because not expected
} catch (\Exception $e) {
        // we don't even reach this exception
	$this->logger->error('Exception encountered while accessing current track');
	$this->logger->error($e);
}

Composer version:

"jwilsson/spotify-web-api-php": "^6.0",

Expectation:
Ideally, when SpotifyAPI returns null because the player is fully exited and the music isn't technically "paused" or "playing", then I should be allowed to check getMyCurrentTrack() for null value(s) and handle my code based around that.

Originally created by @joeygallegos on GitHub (Dec 14, 2023). Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/269 I am writing an application that tracks what song I am currently listening to. When I stop playing music and exit the desktop app, I get the below TypeError exception because the API is returning a null response but the function is declared to only allow `array|object` as return types. Right now, when I wrap the code in a try/catch, it doesn't even run the caught exception. **When playing music, I get no error.** **Exception:** ```Details Type: TypeError Code: 0 Message: SpotifyWebAPI\SpotifyWebAPI::getMyCurrentTrack(): Return value must be of type object|array, null returned File: D:\Dropbox\Web Work\joey\vendor\jwilsson\spotify-web-api-php\src\SpotifyWebAPI.php Line: 1149 ``` **I have my code setup like this:** ``` $options = [ 'auto_refresh' => true, 'return_assoc' => true ]; $api = new SpotifyWebAPI\SpotifyWebAPI($options, $session); ``` ``` try { $spotify = $this->container->get('spotify'); $currentTrack = $spotify['api']->getMyCurrentTrack(); // TODO: Exception when no music is playing. Null value comes forward, but function throws exception because not expected } catch (\Exception $e) { // we don't even reach this exception $this->logger->error('Exception encountered while accessing current track'); $this->logger->error($e); } ``` **Composer version:** ``` "jwilsson/spotify-web-api-php": "^6.0", ``` **Expectation:** Ideally, when SpotifyAPI returns null because the player is fully exited and the music isn't technically "paused" or "playing", then I should be allowed to check `getMyCurrentTrack()` for null value(s) and handle my code based around that.
kerem 2026-02-27 19:26:33 +03:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@jwilsson commented on GitHub (Dec 14, 2023):

Hey!
I totally agree, looks like the Spotify docs doesn't reflect reality. The endpoint returns a HTTP 204 with an empty body when nothing is playing which of course will cause null to be returned.

I'll update the function declaration to reflect this and I've posted on Spotify's developer forum asking them to update the docs.

<!-- gh-comment-id:1856424766 --> @jwilsson commented on GitHub (Dec 14, 2023): Hey! I totally agree, looks like the [Spotify docs](https://developer.spotify.com/documentation/web-api/reference/get-the-users-currently-playing-track) doesn't reflect reality. The endpoint returns a HTTP 204 with an empty body when nothing is playing which of course will cause `null` to be returned. I'll update the function declaration to reflect this and I've [posted on Spotify's developer forum](https://community.spotify.com/t5/Spotify-for-Developers/Missing-documentation-on-Get-Currently-Playing-Track-response/m-p/5747958#M11875) asking them to update the docs.
Author
Owner

@jwilsson commented on GitHub (Dec 14, 2023):

And updated function declarations are available in 6.0.2.

<!-- gh-comment-id:1856434452 --> @jwilsson commented on GitHub (Dec 14, 2023): And updated function declarations are available in `6.0.2`.
Author
Owner

@joeygallegos commented on GitHub (Dec 14, 2023):

@jwilsson many thanks for fixing the issue! I tried again with the new version, and the issue is resolved. Working as expected.

<!-- gh-comment-id:1856630981 --> @joeygallegos commented on GitHub (Dec 14, 2023): @jwilsson many thanks for fixing the issue! I tried again with the new version, and the issue is resolved. Working as expected.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotify-web-api-php#198
No description provided.