mirror of
https://github.com/jwilsson/spotify-web-api-php.git
synced 2026-04-27 07:55:49 +03:00
[GH-ISSUE #119] Unable to catch SpotifyWebAPI\SpotifyWebAPIException #72
Labels
No labels
bug
docs
enhancement
enhancement
enhancement
feedback wanted
good first issue
help wanted
help wanted
help wanted
invalid
pull-request
question
question
upstream
upstream
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/spotify-web-api-php#72
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @JohnGeorgine on GitHub (Apr 1, 2018).
Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/119
The problem here seems kinda simple, yet I'm not being able to solve it.
I'm completely unable to catch exceptions from the API. I'm trying to catch it in order to verify if it's returning a 401 error (so I'll be able to know when the access token has expired and request a new one using the refresh token), but no matter what I do, I keep receiving the fatal error "Uncaught SpotifyWebAPI\SpotifyWebAPIException: The access token expired in...".
I've already tried to use:
"catch(SpotifyWebAPI\SpotifyWebAPIException $e)",
"catch(\SpotifyWebAPI\SpotifyWebAPIException $e)",
"catch(SpotifyWebAPIException $e)",
"catch(\SpotifyWebAPIException $e)",
"catch(Exception $e)" (this one borrowed from the own developer's example provided in https://github.com/jwilsson/spotify-web-api-php/blob/master/docs/examples/handling-errors.md),
and "catch(\Exception $e)",
but no matter what, nothing will do.
In all of the other issues submited here, the developer actually tried to solve the problem causing the Exception instead of dealing with the Exception itself, but the point is I actually have to deal with the Exception.
Any help will be deeply appreciated. Thanks in advance!
@JohnGeorgine commented on GitHub (Apr 1, 2018):
In case it's necessary, my code is:
@jwilsson commented on GitHub (Apr 1, 2018):
Hi!
Have you tried putting the
try/catchblock around the actual API call? Because it's that method that will actually throw an exception when you need to refresh the token. The$api->setAccessToken()call will never throw.So the last line of your code should look like this:
BTW, I edited your comment and put the code in braces so it's easier to read. Hope that's alright!
@JohnGeorgine commented on GitHub (Apr 1, 2018):
OMG, that was it! It's fixed now! I feel so dumb but at the same time glad it worked, LOL. It took me a while to give a reply because I wanted to make sure everything was working properly, since right after the change I was getting a blank page for some reason. But now everything seems to be fine. I even made some tests by setting a custom message and a different API output for the "catch" block, so I'd know exactly when the key was expired and a new one was generated, and it all ran smoothly. Thank you and congrats for your API!
@JohnGeorgine commented on GitHub (Apr 1, 2018):
BTW, that's okay with the braces. I'm new to GitHub, so I wasn't sure how to properly format my code. Thanks again!