[GH-ISSUE #207] Fatal error when invalid ID is used #143

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

Originally created by @samcera on GitHub (Nov 10, 2020).
Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/207

When I use the error handling code (as described in the example doc and posted below) I receive a fatal error (posted below as well). Judging from issue #176 it seems that this is expected behavior for invalid ID's. However, if that's the case I don't seem to understand what the code below does. It's not showing me a echoed 404 in the response (or in the headers). Is this still expected behavior for invalid ID's or am I missing something here? I intended to check if the ID I use to call Spotify is valid, before proceeding with the other code. Any help is greatly appreciated :)

try {
    $track = $api->getTrack('non-existing-track');
} catch (SpotifyWebAPIException $e) {
    echo 'Spotify API Error: ' . $e->getCode(); // Will be 404
}
Fatal error: Uncaught SpotifyWebAPI\SpotifyWebAPIException: non existing id in C:\wamp\www\test\vendor\jwilsson\spotify-web-api-php\src\Request.php on line 56
( ! ) SpotifyWebAPI\SpotifyWebAPIException: non existing id in C:\wamp\www\test\vendor\jwilsson\spotify-web-api-php\src\Request.php on line 56
Call Stack
#	Time	Memory	Function	Location
1	0.0019	413544	{main}( )	...\app.php:0
2	0.0033	430200	SpotifyWebAPI\SpotifyWebAPI->getTrack( )	...\app.php:33
3	0.0033	430264	SpotifyWebAPI\SpotifyWebAPI->sendRequest( )	...\SpotifyWebAPI.php:1348

Originally created by @samcera on GitHub (Nov 10, 2020). Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/207 When I use the error handling code (as described in the example doc and posted below) I receive a fatal error (posted below as well). Judging from issue #176 it seems that this is expected behavior for invalid ID's. However, if that's the case I don't seem to understand what the code below does. It's not showing me a echoed 404 in the response (or in the headers). Is this still expected behavior for invalid ID's or am I missing something here? I intended to check if the ID I use to call Spotify is valid, before proceeding with the other code. Any help is greatly appreciated :) ``` try { $track = $api->getTrack('non-existing-track'); } catch (SpotifyWebAPIException $e) { echo 'Spotify API Error: ' . $e->getCode(); // Will be 404 } ``` ``` Fatal error: Uncaught SpotifyWebAPI\SpotifyWebAPIException: non existing id in C:\wamp\www\test\vendor\jwilsson\spotify-web-api-php\src\Request.php on line 56 ( ! ) SpotifyWebAPI\SpotifyWebAPIException: non existing id in C:\wamp\www\test\vendor\jwilsson\spotify-web-api-php\src\Request.php on line 56 Call Stack # Time Memory Function Location 1 0.0019 413544 {main}( ) ...\app.php:0 2 0.0033 430200 SpotifyWebAPI\SpotifyWebAPI->getTrack( ) ...\app.php:33 3 0.0033 430264 SpotifyWebAPI\SpotifyWebAPI->sendRequest( ) ...\SpotifyWebAPI.php:1348 ```
kerem 2026-02-27 19:26:17 +03:00
  • closed this issue
  • added the
    docs
    label
Author
Owner

@jwilsson commented on GitHub (Nov 11, 2020):

Hey!
It's a slight error in the documentation. The SpotifyWebAPIException class is missing its namespace.

Adding that will make it work:

try {
    $track = $api->getTrack('non-existing-track');
} catch (SpotifyWebAPI\SpotifyWebAPIException $e) {
    echo 'Spotify API Error: ' . $e->getCode(); // Will be 404
}
<!-- gh-comment-id:725210417 --> @jwilsson commented on GitHub (Nov 11, 2020): Hey! It's a slight error in the documentation. The `SpotifyWebAPIException` class is missing its namespace. Adding that will make it work: ```php try { $track = $api->getTrack('non-existing-track'); } catch (SpotifyWebAPI\SpotifyWebAPIException $e) { echo 'Spotify API Error: ' . $e->getCode(); // Will be 404 } ```
Author
Owner

@samcera commented on GitHub (Nov 11, 2020):

Thanks! Working beautifully now 👍

<!-- gh-comment-id:725289390 --> @samcera commented on GitHub (Nov 11, 2020): Thanks! Working beautifully now 👍
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#143
No description provided.