From ca3c506fb41f1dd489c7eb9219c35534da491f78 Mon Sep 17 00:00:00 2001 From: Mike Lang Date: Sat, 3 Aug 2024 15:18:52 +0000 Subject: [PATCH] playlist_manager: Make a specialized error for api requests So we can easily check the status code. --- playlist_manager/playlist_manager/main.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/playlist_manager/playlist_manager/main.py b/playlist_manager/playlist_manager/main.py index 60ef2c4..7f8e405 100644 --- a/playlist_manager/playlist_manager/main.py +++ b/playlist_manager/playlist_manager/main.py @@ -18,6 +18,13 @@ PlaylistConfig = namedtuple("Playlist", ["tags", "first_event_id", "last_event_i PlaylistEntry = namedtuple("PlaylistEntry", ["entry_id", "video_id"]) +class APIException(Exception): + """Thrown when an API call fails. Exposes the HTTP status code.""" + def __init__(self, message, code): + super().__init__(message) + self.code = code + + class PlaylistManager(object): def __init__(self, dbmanager, api_client, upload_locations, playlist_tags): @@ -246,9 +253,9 @@ class YoutubeAPI(object): metric_name="playlist_insert", ) if not resp.ok: - raise Exception("Failed to insert {video_id} at index {index} of {playlist} with {resp.status_code}: {resp.content}".format( + raise APIException("Failed to insert {video_id} at index {index} of {playlist} with {resp.status_code}: {resp.content}".format( playlist=playlist_id, video_id=video_id, index=index, resp=resp, - )) + ), code=resp.status_code) # TODO return entry_id from resp def list_playlist(self, playlist_id): @@ -272,9 +279,9 @@ class YoutubeAPI(object): metric_name="playlist_list", ) if not resp.ok: - raise Exception("Failed to list {playlist} (page_token={page_token!r}) with {resp.status_code}: {resp.content}".format( + raise APIException("Failed to list {playlist} (page_token={page_token!r}) with {resp.status_code}: {resp.content}".format( playlist=playlist_id, page_token=page_token, resp=resp, - )) + ), code=resp.status_code) return resp.json()