Skip to content

Commit 23221a2

Browse files
committed
Fix previous commit.
- Return only if hosting method is HEROKU. - Try to get the response error message first if any.
1 parent fa135eb commit 23221a2

File tree

3 files changed

+47
-21
lines changed

3 files changed

+47
-21
lines changed

bot.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,17 +1594,22 @@ async def autoupdate(self):
15941594
latest = changelog.latest_version
15951595

15961596
if self.version < parse_version(latest.version):
1597+
error = None
1598+
data = {}
15971599
try:
15981600
# update fork if gh_token exists
15991601
data = await self.api.update_repository()
16001602
except InvalidConfigError:
1601-
data = {}
1603+
pass
16021604
except ClientResponseError as exc:
1603-
logger.error(f"Autoupdate failed! Status {exc.status}.")
1604-
logger.error(f"Message: {exc.message}")
1605-
self.autoupdate.cancel()
1606-
return
1605+
error = exc
16071606
if self.hosting_method == HostingMethod.HEROKU:
1607+
if error is not None:
1608+
logger.error(f"Autoupdate failed! Status: {error.status}.")
1609+
logger.error(f"Error message: {error.message}")
1610+
self.autoupdate.cancel()
1611+
return
1612+
16081613
commit_data = data.get("data")
16091614
if not commit_data:
16101615
return
@@ -1681,15 +1686,18 @@ async def before_autoupdate(self):
16811686
if self.config.get("disable_autoupdates"):
16821687
logger.warning("Autoupdates disabled.")
16831688
self.autoupdate.cancel()
1689+
return
16841690

16851691
if self.hosting_method == HostingMethod.DOCKER:
16861692
logger.warning("Autoupdates disabled as using Docker.")
16871693
self.autoupdate.cancel()
1694+
return
16881695

16891696
if not self.config.get("github_token") and self.hosting_method == HostingMethod.HEROKU:
16901697
logger.warning("GitHub access token not found.")
16911698
logger.warning("Autoupdates disabled.")
16921699
self.autoupdate.cancel()
1700+
return
16931701

16941702
def format_channel_name(self, author, exclude_channel=None, force_null=False):
16951703
"""Sanitises a username for use with text channel names

cogs/utility.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,20 +1951,24 @@ async def update(self, ctx, *, flag: str = ""):
19511951
embed.set_author(name=user["username"], icon_url=user["avatar_url"], url=user["url"])
19521952
await ctx.send(embed=embed)
19531953
else:
1954+
error = None
1955+
data = {}
19541956
try:
19551957
# update fork if gh_token exists
19561958
data = await self.bot.api.update_repository()
19571959
except InvalidConfigError:
1958-
data = {}
1960+
pass
19591961
except ClientResponseError as exc:
1960-
embed = discord.Embed(
1961-
title="Update failed",
1962-
description=f"Error status {exc.status}. {exc.message}",
1963-
color=self.bot.error_color,
1964-
)
1965-
return await ctx.send(embed=embed)
1962+
error = exc
19661963

19671964
if self.bot.hosting_method == HostingMethod.HEROKU:
1965+
if error is not None:
1966+
embed = discord.Embed(
1967+
title="Update failed",
1968+
description=f"Error status: {error.status}.\nError message: {error.message}",
1969+
color=self.bot.error_color,
1970+
)
1971+
return await ctx.send(embed=embed)
19681972
if not data:
19691973
# invalid gh_token
19701974
embed = discord.Embed(

core/clients.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,18 @@ async def request(
130130
await resp.read()
131131
return resp
132132

133-
try:
134-
return await resp.json()
135-
except (JSONDecodeError, ClientResponseError):
136-
return await resp.text()
133+
return await self._get_response_data(resp)
134+
135+
@staticmethod
136+
async def _get_response_data(response: ClientResponse) -> Union[Dict[str, Any], str]:
137+
"""
138+
Internal method to convert the response data to `dict` if the data is a
139+
json object, or to `str` (raw response) if the data is not a valid json.
140+
"""
141+
try:
142+
return await response.json()
143+
except (JSONDecodeError, ClientResponseError):
144+
return await response.text()
137145

138146
def filter_valid(self, data) -> Dict[str, Any]:
139147
"""
@@ -198,14 +206,20 @@ async def update_repository(self, sha: str = None) -> Dict[str, Any]:
198206
# source https://docs.github.com/en/rest/branches/branches#merge-a-branch
199207

200208
status = resp.status
209+
data = await self._get_response_data(resp)
201210
if status in (201, 204):
202-
try:
203-
return await resp.json()
204-
except (JSONDecodeError, ClientResponseError):
205-
return await resp.text()
211+
return data
206212

207213
args = (resp.request_info, resp.history)
208-
kwargs = {"status": status, "message": status_map.get(status)}
214+
try:
215+
# try to get the response error message if any
216+
message = data.get("message")
217+
except AttributeError:
218+
message = None
219+
kwargs = {
220+
"status": status,
221+
"message": message if message else status_map.get(status),
222+
}
209223
# just raise
210224
raise ClientResponseError(*args, **kwargs)
211225

0 commit comments

Comments
 (0)