diff --git a/src/exceptions.py b/src/exceptions.py index d837e30..c311867 100644 --- a/src/exceptions.py +++ b/src/exceptions.py @@ -58,6 +58,11 @@ class BadRequest(Exception): self.message = f"params must be either a strong or OrderedDict object, not {type(object_type)}!" 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): """When MediaWiki responds with an error""" diff --git a/src/wiki.py b/src/wiki.py index 47f115b..ffcf7f3 100644 --- a/src/wiki.py +++ b/src/wiki.py @@ -369,10 +369,11 @@ class Wiki: if 499 < request.status < 600: logger.warning(f"A request to {self.script_url} {params} resulted in {request.status}") raise ServerError - elif request.status == 302: + elif request.status in (301, 302): 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( request.url)) + raise ServerRedirects(self.script_url, request.headers.get("Location", "unknown")) elif request.status in (410, 404): raise WikiNotFoundError(request.status) elif 399 < request.status < 500: @@ -458,7 +459,7 @@ class Wiki: "rclimit": amount, "rctype": "edit|new|log|categorize", "siprop": "namespaces|general"}) try: 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)) raise WikiServerError(e) return response