aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar kwzrd <[email protected]>2021-03-26 14:44:46 +0100
committerGravatar kwzrd <[email protected]>2021-03-26 14:54:30 +0100
commitc14d9ea78a64b90ccf7815a71206c906c81af710 (patch)
tree9f04d2662fe678b628fd4043de29652a2476901a
parentBranding: cache fresh event description in daemon (diff)
Branding: raise on non-200 responses
The fetch helpers will now raise when the request fails rather than logging a warning and returning a fallback value. This allows better error logging as the caller is able to log the propagated exception while adding its own context. Additionally, the caller in some cases no longer needs to check for the None return and raise its own exception.
-rw-r--r--bot/exts/backend/branding/_cog.py8
-rw-r--r--bot/exts/backend/branding/_repository.py35
2 files changed, 20 insertions, 23 deletions
diff --git a/bot/exts/backend/branding/_cog.py b/bot/exts/backend/branding/_cog.py
index 7d4f80f13..d6c5b159b 100644
--- a/bot/exts/backend/branding/_cog.py
+++ b/bot/exts/backend/branding/_cog.py
@@ -145,10 +145,10 @@ class Branding(commands.Cog):
"""
log.info(f"Applying {asset_type.value} asset to the guild")
- file = await self.repository.fetch_file(download_url)
-
- if file is None:
- log.error(f"Failed to download {asset_type.value} from branding repository!")
+ try:
+ file = await self.repository.fetch_file(download_url)
+ except Exception as fetch_exc:
+ log.error(f"Failed to fetch '{asset_type.value}' asset: {fetch_exc}")
return False
await self.bot.wait_until_guild_available()
diff --git a/bot/exts/backend/branding/_repository.py b/bot/exts/backend/branding/_repository.py
index e14ff4226..715361c5d 100644
--- a/bot/exts/backend/branding/_repository.py
+++ b/bot/exts/backend/branding/_repository.py
@@ -100,33 +100,30 @@ class BrandingRepository:
The directory will be represented by a mapping from file or sub-directory names to their corresponding
instances of `RemoteObject`. Passing a custom `types` value allows only getting files or directories.
- If the request fails, returns an empty dictionary.
+ An exception will be raised if the request fails, or if the response lacks the expected keys.
"""
full_url = f"{BRANDING_URL}/{path}"
log.debug(f"Fetching directory from branding repository: {full_url}")
async with self.bot.http_session.get(full_url, params=PARAMS, headers=HEADERS) as response:
- if response.status == 200:
- json_directory = await response.json()
- else:
- log.warning(f"Received non-200 response status: {response.status}")
- return {}
+ if response.status != 200:
+ raise RuntimeError(f"Failed to fetch directory due to status: {response.status}")
+ json_directory = await response.json()
return {file["name"]: RemoteObject(file) for file in json_directory if file["type"] in types}
- async def fetch_file(self, download_url: str) -> t.Optional[bytes]:
+ async def fetch_file(self, download_url: str) -> bytes:
"""
- Fetch file from `download_url`.
+ Fetch file as bytes from `download_url`.
- Returns the file as bytes unless the request fails, in which case None is given.
+ Raise an exception if the request does not succeed.
"""
log.debug(f"Fetching file from branding repository: {download_url}")
async with self.bot.http_session.get(download_url, params=PARAMS, headers=HEADERS) as response:
- if response.status == 200:
- return await response.read()
- else:
- log.warning(f"Received non-200 response status: {response.status}")
+ if response.status != 200:
+ raise RuntimeError(f"Failed to fetch file due to status: {response.status}")
+ return await response.read()
async def parse_meta_file(self, raw_file: bytes) -> MetaFile:
"""
@@ -170,16 +167,11 @@ class BrandingRepository:
server_icons = await self.fetch_directory(contents["server_icons"].path, types=("file",))
- if server_icons is None:
- raise BrandingMisconfiguration("Failed to fetch server icons!")
if len(server_icons) == 0:
raise BrandingMisconfiguration("Found no server icons!")
meta_bytes = await self.fetch_file(contents["meta.md"].download_url)
- if meta_bytes is None:
- raise BrandingMisconfiguration("Failed to fetch 'meta.md' file!")
-
meta_file = await self.parse_meta_file(meta_bytes)
return Event(directory.path, meta_file, contents["banner.png"], list(server_icons.values()))
@@ -193,7 +185,12 @@ class BrandingRepository:
"""
log.debug("Discovering events in branding repository")
- event_directories = await self.fetch_directory("events", types=("dir",)) # Skip files
+ try:
+ event_directories = await self.fetch_directory("events", types=("dir",)) # Skip files
+ except Exception as fetch_exc:
+ log.error(f"Failed to fetch 'events' directory: {fetch_exc}")
+ return []
+
instances: t.List[Event] = []
for event_directory in event_directories.values():