Skip to content

Commit 370b9a0

Browse files
committed
exceptions: Add helper functions to retrieve error msg/codes
The XMLRPC APIs raise Fault errors, and we can't change that otherwise it breaks API. REST will use regular BugzillaError. Add some helper functions that can be used to get the error code and message from either variant Signed-off-by: Cole Robinson <[email protected]>
1 parent b428676 commit 370b9a0

2 files changed

Lines changed: 33 additions & 8 deletions

File tree

bugzilla/base.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
if sys.version_info[0] >= 3:
2020
from collections.abc import Mapping
2121
from urllib.parse import urlparse, urlunparse, parse_qsl
22-
from xmlrpc.client import Binary, Fault
22+
from xmlrpc.client import Binary
2323
else:
2424
from collections import Mapping
2525
from urlparse import urlparse, urlunparse, parse_qsl
26-
from xmlrpclib import Binary, Fault
26+
from xmlrpclib import Binary
2727
# pylint: enable=import-error,no-name-in-module,ungrouped-imports
2828

2929

@@ -267,7 +267,7 @@ def _init_class_from_url(self):
267267
log.info("Found RedHat bugzilla extension, "
268268
"using RHBugzilla")
269269
c = RHBugzilla
270-
except Fault:
270+
except Exception:
271271
log.debug("Failed to fetch bugzilla extensions", exc_info=True)
272272

273273
if not c:
@@ -558,8 +558,9 @@ def login(self, user=None, password=None, restrict_login=None):
558558
self.password = ''
559559
log.info("login successful for user=%s", self.user)
560560
return ret
561-
except Fault as e:
562-
raise BugzillaError("Login failed: %s" % str(e.faultString))
561+
except Exception as e:
562+
raise BugzillaError("Login failed: %s" %
563+
BugzillaError.get_bugzilla_error_string(e))
563564

564565
def interactive_login(self, user=None, password=None, force=False,
565566
restrict_login=None, use_api_key=False):
@@ -639,8 +640,9 @@ def logged_in(self):
639640
try:
640641
self._backend.user_get({"ids": []})
641642
return True
642-
except Fault as e:
643-
if e.faultCode == 505 or e.faultCode == 32000:
643+
except Exception as e:
644+
code = BugzillaError.get_bugzilla_error_code(e)
645+
if code in [505, 32000]:
644646
return False
645647
raise e
646648

@@ -1235,7 +1237,9 @@ def query(self, query):
12351237
"""
12361238
try:
12371239
r = self._backend.bug_search(query)
1238-
except Fault as e:
1240+
except Exception as e:
1241+
if not BugzillaError.get_bugzilla_error_code(e):
1242+
raise
12391243

12401244
# Try to give a hint in the error message if url_to_query
12411245
# isn't supported by this bugzilla instance

bugzilla/exceptions.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,24 @@ class BugzillaError(Exception):
66
"""
77
Error raised in the Bugzilla client code.
88
"""
9+
@staticmethod
10+
def get_bugzilla_error_string(exc):
11+
"""
12+
Helper to return the bugzilla instance error message from an
13+
XMLRPC Fault, or any other exception type that's raised from bugzilla
14+
interaction
15+
"""
16+
if hasattr(exc, "faultString"):
17+
return getattr(exc, "faultString")
18+
return str(exc)
19+
20+
@staticmethod
21+
def get_bugzilla_error_code(exc):
22+
"""
23+
Helper to return the bugzilla instance error code from an
24+
XMLRPC Fault, or any other exception type that's raised from bugzilla
25+
interaction
26+
"""
27+
if hasattr(exc, "faultCode"):
28+
return getattr(exc, "faultCode")
29+
return None

0 commit comments

Comments
 (0)