77import os
88
99from ._backendbase import _BackendBase
10- from .exceptions import BugzillaError
10+ from .exceptions import BugzillaError , BugzillaHTTPError
1111from ._util import listify
1212
1313
@@ -32,6 +32,23 @@ def __init__(self, url, bugzillasession):
3232 #########################
3333 # Internal REST helpers #
3434 #########################
35+ def _handle_error (self , e ):
36+ response = getattr (e , "response" , None )
37+ if response is None :
38+ raise e # pragma: no cover
39+
40+ if response .status_code in [400 , 401 , 404 ]:
41+ self ._handle_error_response (response .text )
42+ raise e
43+
44+ def _handle_error_response (self , text ):
45+ try :
46+ result = json .loads (text )
47+ except json .JSONDecodeError :
48+ return
49+
50+ if result .get ("error" ):
51+ raise BugzillaError (result ["message" ], code = result ["code" ])
3552
3653 def _handle_response (self , text ):
3754 try :
@@ -40,7 +57,7 @@ def _handle_response(self, text):
4057 log .debug ("Failed to parse REST response. Output is:\n %s" , text )
4158 raise
4259
43- if ret .get ("error" , False ):
60+ if ret .get ("error" , False ): # pragma: no cover
4461 raise BugzillaError (ret ["message" ], code = ret ["code" ])
4562 return ret
4663
@@ -55,8 +72,13 @@ def _op(self, method, apiurl, paramdict=None):
5572 else :
5673 data = json .dumps (paramdict or {})
5774
58- response = self ._bugzillasession .request (method , fullurl , data = data ,
59- params = authparams )
75+ try :
76+ response = self ._bugzillasession .request (
77+ method , fullurl , data = data , params = authparams
78+ )
79+ except BugzillaHTTPError as e :
80+ self ._handle_error (e )
81+
6082 return self ._handle_response (response .text )
6183
6284 def _get (self , * args , ** kwargs ):
0 commit comments