Made better handling for redirects

This commit is contained in:
Frisk 2025-01-29 00:21:47 +01:00
parent c6a846fe18
commit 2dc3ec12c3
2 changed files with 8 additions and 2 deletions

View file

@ -58,6 +58,11 @@ class BadRequest(Exception):
self.message = f"params must be either a strong or OrderedDict object, not {type(object_type)}!" self.message = f"params must be either a strong or OrderedDict object, not {type(object_type)}!"
super().__init__(self.message) super().__init__(self.message)
class ServerRedirects(Exception):
"""When MediaWiki responds with an error"""
def __init__(self, old_url, new_url):
self.message = f"Wiki with script of {old_url} redirects to {new_url}!"
super().__init__(self.message)
class MediaWikiError(Exception): class MediaWikiError(Exception):
"""When MediaWiki responds with an error""" """When MediaWiki responds with an error"""

View file

@ -369,10 +369,11 @@ class Wiki:
if 499 < request.status < 600: if 499 < request.status < 600:
logger.warning(f"A request to {self.script_url} {params} resulted in {request.status}") logger.warning(f"A request to {self.script_url} {params} resulted in {request.status}")
raise ServerError raise ServerError
elif request.status == 302: elif request.status in (301, 302):
logger.critical( logger.critical(
"Redirect detected! Either the wiki given in the script settings (wiki field) is incorrect/the wiki got removed or is giving us the false value. Please provide the real URL to the wiki, current URL redirects to {}".format( "Redirect detected! Either the wiki given in the script settings (wiki field) is incorrect/the wiki got removed or is giving us the false value. Please provide the real URL to the wiki, current URL redirects to {}".format(
request.url)) request.url))
raise ServerRedirects(self.script_url, request.headers.get("Location", "unknown"))
elif request.status in (410, 404): elif request.status in (410, 404):
raise WikiNotFoundError(request.status) raise WikiNotFoundError(request.status)
elif 399 < request.status < 500: elif 399 < request.status < 500:
@ -458,7 +459,7 @@ class Wiki:
"rclimit": amount, "rctype": "edit|new|log|categorize", "siprop": "namespaces|general"}) "rclimit": amount, "rctype": "edit|new|log|categorize", "siprop": "namespaces|general"})
try: try:
response = await self.api_request(params=params) response = await self.api_request(params=params)
except (aiohttp.ClientConnectionError, aiohttp.ServerTimeoutError, asyncio.TimeoutError) as e: except (aiohttp.ClientConnectionError, aiohttp.ServerTimeoutError, asyncio.TimeoutError, ServerRedirects) as e:
logger.error("A connection error occurred while requesting {}".format(params)) logger.error("A connection error occurred while requesting {}".format(params))
raise WikiServerError(e) raise WikiServerError(e)
return response return response