spotify: Handle API failures - #5910
Conversation
|
Thank you for the PR! The changelog has not been updated, so here is a friendly reminder to check if you need to add an entry. |
There was a problem hiding this comment.
Pull Request Overview
This PR improves error handling in the Spotify plugin by adding proper exception handling around API calls that previously could crash during imports. The changes wrap critical Spotify API interactions with try-catch blocks to gracefully handle API failures.
Key changes:
- Added exception handling for API failures in album and track retrieval methods
- Modified
track_infomethod to returnNoneon API errors instead of crashing - Updated type annotations to reflect the new nullable return types
| def track_info(self, track_id: str): | ||
| def track_info( | ||
| self, track_id: str | ||
| ) -> Tuple[Any | None, Any | None, Any | None, Any | None] | None: |
There was a problem hiding this comment.
[nitpick] The return type annotation is verbose and unclear. Consider creating a type alias or using a more descriptive approach, such as TrackInfoTuple | None where TrackInfoTuple = Tuple[Optional[Any], Optional[Any], Optional[Any], Optional[Any]].
| ) -> Tuple[Any | None, Any | None, Any | None, Any | None] | None: | |
| ) -> TrackInfoTuple | None: |
Codecov Report❌ Patch coverage is
🚀 New features to boost your workflow:
|
dc5535d to
08b239f
Compare
| try: | ||
| album_data = self._handle_response( | ||
| "get", self.album_url + spotify_id | ||
| ) | ||
| except APIError: | ||
| return None | ||
|
|
There was a problem hiding this comment.
This approach introduces a lot of duplication in the code. I think a cleaner approach would be to never raise APIError in the first place.
See _handle_response method - I'd suggest removing all lines that raise APIError and returning None instead.
|
Maybe it is worth taking a step back here: How do we want to handle errors in metadata plugins in general? E.g. what should happen if one plugin fails because a remote service is currently unreachable? Do we want beets to crash here or just warn a user? I think raising errors in the specific metadata plugin (such as spotify or deezer) is completely fine. We should handle errors globally for any metadata plugins tho and warn users. Proposal: Let's add error handling to the |
08b239f to
55b3e45
Compare
|
@9999years Are you planning to keep looking into the metadataplugin layer for adding error handling? If you want to take the lead on it, go ahead. I’d be glad to chime in or help out later, since it’s also needed for the aisauce plugin. From what I can tell it shouldn’t be too difficult to add. |
|
@semohr Feel free, I might get back to this in the future but I'm not sure when. |
Lots of these weren't handled, leading to crashes while importing.
55b3e45 to
bf927a5
Compare
When a metadata plugin raises an exception during the auto-tagger process, the entire operation crashes. This behavior is not desirable, since metadata lookups can legitimately fail for various reasons (e.g., temporary API downtime, network issues, or offline usage). This PR introduces a safeguard by adding general exception handling around metadata plugin calls. Instead of causing the whole process to fail, exceptions from individual plugins are now caught and logged. This ensures that the auto-tagger continues to function with the remaining available metadata sources. I used a proxy pattern here as this seems like an elegant solution to me. This replaces the efforts from #5910
Lots of these weren't handled, leading to crashes while importing.
To Do
docs/to describe it.)docs/changelog.rstto the bottom of one of the lists near the top of the document.)