jenkins-bot has submitted this change. (
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/663784 )
Change subject: [IMPR] Raise an exception response is Non-JSON and site is AutoFamily
......................................................................
[IMPR] Raise an exception response is Non-JSON and site is AutoFamily
- Raise a SiteDefinitionError if api request response is not a json
content and the site.family is AutoFamily class
- Print additional informations if Non-JSON response received from
server either as debug message as warning
Bug: T272911
Change-Id: I874214ba0d6d1cb5ee3436981a2177e445515562
---
M pywikibot/data/api.py
M tests/link_tests.py
2 files changed, 39 insertions(+), 14 deletions(-)
Approvals:
JJMC89: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/data/api.py b/pywikibot/data/api.py
index ae28af0..d5a0e27 100644
--- a/pywikibot/data/api.py
+++ b/pywikibot/data/api.py
@@ -31,11 +31,21 @@
from pywikibot.backports import removeprefix, Tuple
from pywikibot.comms import http
from pywikibot.exceptions import (
- CaptchaError, Server504Error, Server414Error, FatalServerError, NoUsername,
- Error, TimeoutError, MaxlagTimeoutError, InvalidTitle, UnsupportedPage
+ CaptchaError,
+ Error,
+ FatalServerError,
+ InvalidTitle,
+ MaxlagTimeoutError,
+ NoUsername,
+ Server414Error,
+ Server504Error,
+ SiteDefinitionError,
+ TimeoutError,
+ UnsupportedPage,
)
from pywikibot.family import SubdomainFamily
from pywikibot.login import LoginStatus
+from pywikibot.textlib import removeHTMLParts
from pywikibot.tools import itergroup, PYTHON_VERSION
from pywikibot.tools.formatter import color_format
@@ -1567,12 +1577,29 @@
try:
result = response.json()
except ValueError:
- # if the result isn't valid JSON, there must be a server
+ # if the result isn't valid JSON, there may be a server
# problem. Wait a few seconds and try again
- pywikibot.warning(
- 'Non-JSON response received from server {}; '
- 'the server may be down.\nStatus code:{}'
- .format(self.site, response.status_code))
+ # Show 20 lines of bare text
+ text = '\n'.join(removeHTMLParts(response.text).splitlines()[:20])
+ msg = """\
+Non-JSON response received from server {site} for url
+{resp.url}
+The server may be down.
+Status code: {resp.status_code}
+
+The text message is:
+{text}
+""".format(site=self.site, resp=response, text=text)
+
+ # Do not retry for AutoFamily but raise a SiteDefinitionError
+ # Note: family.AutoFamily is a function to create that class
+ if self.site.family.__class__.__name__ == 'AutoFamily':
+ pywikibot.debug(msg, _logger)
+ raise SiteDefinitionError('Invalid AutoFamily({!r})'
+ .format(self.site.family.domain))
+
+ pywikibot.warning(msg)
+
# there might also be an overflow, so try a smaller limit
for param in self._params:
if param.endswith('limit'):
diff --git a/tests/link_tests.py b/tests/link_tests.py
index 92a52b2..d3550bc 100644
--- a/tests/link_tests.py
+++ b/tests/link_tests.py
@@ -14,7 +14,7 @@
from pywikibot import Site
from pywikibot.page import Link, Page, SiteLink
from pywikibot.site import Namespace
-from pywikibot.exceptions import InvalidTitle, TimeoutError
+from pywikibot.exceptions import InvalidTitle, SiteDefinitionError
from tests.aspects import (
unittest,
@@ -932,12 +932,10 @@
"""Test that Link fails if the interwiki prefix is not a
wiki."""
link = Link('bugzilla:1337', source=self.site)
# bugzilla does not return a json content but redirects to phab.
- # api.Request._json_loads cannot detect this problem and retries
- # reloading due to 'the server may be down'
-
- # ignore Timeout when trying to load siteninfo;
- # the site is created anyway but the title cannot be parsed
- with suppress(TimeoutError):
+ # api.Request._json_loads cannot detect this problem and raises
+ # a SiteDefinitionError. The site is created anyway but the title
+ # cannot be parsed
+ with self.assertRaises(SiteDefinitionError):
link.site
self.assertEqual(link.site.sitename, 'wikimedia:wikimedia')
self.assertTrue(link._is_interwiki)
--
To view, visit
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/663784
To unsubscribe, or for help writing mail filters, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I874214ba0d6d1cb5ee3436981a2177e445515562
Gerrit-Change-Number: 663784
Gerrit-PatchSet: 7
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: JAn Dudík <jan.dudik(a)gmail.com>
Gerrit-Reviewer: JJMC89 <JJMC89.Wikimedia(a)gmail.com>
Gerrit-Reviewer: Matěj Suchánek <matejsuchanek97(a)gmail.com>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged