jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/349156 )
Change subject: site_detect.py: Check the response in _parse_post_117 for server errors
......................................................................
site_detect.py: Check the response in _parse_post_117 for server errors
Bug: T160355
Change-Id: I6e762778327de02a01772c649f2b22d2e4dd96d7
---
M pywikibot/site_detect.py
1 file changed, 14 insertions(+), 6 deletions(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/pywikibot/site_detect.py b/pywikibot/site_detect.py
index d77a953..d4bf878 100644
--- a/pywikibot/site_detect.py
+++ b/pywikibot/site_detect.py
@@ -56,12 +56,7 @@
if fromurl.endswith("$1"):
fromurl = fromurl[:-2]
r = fetch(fromurl)
- if r.status == 503:
- raise ServerError('Service Unavailable')
- elif r.status == 500:
- raise ServerError('Internal Server Error')
- elif r.status == 200 and SERVER_DB_ERROR_MSG in r.content:
- raise ServerError('Server cannot access the database')
+ check_response(r)
if fromurl != r.data.url:
pywikibot.log('{0} redirected to {1}'.format(fromurl, r.data.url))
@@ -87,6 +82,8 @@
if self.api:
try:
self._parse_post_117()
+ except ServerError:
+ raise
except Exception as e:
pywikibot.log('MW 1.17+ detection failed: {0!r}'.format(e))
@@ -169,6 +166,7 @@
def _parse_post_117(self):
"""Parse 1.17+ siteinfo data."""
response = fetch(self.api +
'?action=query&meta=siteinfo&format=json')
+ check_response(response)
# remove preleading newlines and Byte Order Mark (BOM), see T128992
content = response.content.strip().lstrip('\uFEFF')
info = json.loads(content)
@@ -299,3 +297,13 @@
self.set_api_url(attrs['href'])
elif tag == 'script' and 'src' in attrs:
self.set_api_url(attrs['src'])
+
+
+def check_response(response):
+ """Raise ServerError if the response indicates a server
error."""
+ if response.status == 503:
+ raise ServerError('Service Unavailable')
+ elif response.status == 500:
+ raise ServerError('Internal Server Error')
+ elif response.status == 200 and SERVER_DB_ERROR_MSG in response.content:
+ raise ServerError('Server cannot access the database')
--
To view, visit
https://gerrit.wikimedia.org/r/349156
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I6e762778327de02a01772c649f2b22d2e4dd96d7
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Dalba <dalba.wiki(a)gmail.com>
Gerrit-Reviewer: Magul <tomasz.magulski(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot <>