mirror of
https://github.com/jwilsson/spotify-web-api-php.git
synced 2026-04-27 07:55:49 +03:00
[GH-ISSUE #129] PHP question - How to wrap every call to Spotify in try-catch #77
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#77
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 @kasperkamperman on GitHub (Jun 11, 2018).
Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/129
I think it might be smart if I wrap every call I make in a try-catch, so I can detect errors (rate-limit, expired accesToken). Like shown in this closed issue: https://github.com/jwilsson/spotify-web-api-php/issues/72
I'm aware that this is more a lack in my PHP knowledge than an issue with this API, however I think maybe more people would like "save" calls, so the answer can be useful for others as well.
An example call that I'd like to wrap would be:
I was think to make a function like callSpotify and then pass the call. However I'm in doubt how to pass the parameters, because I only could find example to functions without parameters.
@jwilsson commented on GitHub (Jun 12, 2018):
Hi!
Try something like this:
That will let you pass any method and any number of arguments to it.
@kasperkamperman commented on GitHub (Jun 12, 2018):
Thanks. I'll try it out.
@andersborgabiro commented on GitHub (Jun 13, 2018):
I do that systematically for all the calls, and in all cases I present my own messages, a la "Failed retrieving album xxxxxx" etc, instead of generic messages.
@kasperkamperman commented on GitHub (Jun 14, 2018):
Maybe for archiving purposes in case someone else runs across this. The function I've ended up with:
Called like:
@kasperkamperman commented on GitHub (Feb 24, 2019):
Sorry to 'open' this up again. I'm using the callSpotify function like this to obtain a new accessToken and then I'll recursively call it again (with the updated accessToken). However if I call it recursively the function doesn't return anything, while there is data.
I tried several things. I've declared a variable outside the try-catch block however I can't seem to access the data outside this block.
I'm using PHP 7.3. I came across some topics on StackOverflow that try/catch works a bit different, however I can't seem to find what's going on.
I hope someone has a clue.
@kasperkamperman commented on GitHub (Feb 24, 2019):
Ok, I found the bug of not returning. In the catch statement I did callSpotify, but I forgot to return the value.
should be
Still wondering though why a variable that will be modified in try-catch is not accessible outside it.