2222from http import HTTPStatus
2323import json
2424from urllib .parse import unquote
25- from requests . exceptions import HTTPError
25+ from requests import HTTPError , RequestException
2626import requests .utils
2727
2828from sonar import platform
@@ -89,8 +89,8 @@ def get_object(cls, concerned_object: projects.Project, branch_name: str) -> Bra
8989 return _OBJECTS [uu ]
9090 try :
9191 data = json .loads (concerned_object .endpoint .get (APIS ["list" ], params = {"project" : concerned_object .key }).text )
92- except HTTPError as e :
93- if e .response .status_code == HTTPStatus .NOT_FOUND :
92+ except ( HTTPError , ConnectionError , RequestException ) as e :
93+ if isinstance ( HTTPError , e ) and e .response .status_code == HTTPStatus .NOT_FOUND :
9494 raise exceptions .ObjectNotFound (concerned_object .key , f"Project '{ concerned_object .key } ' not found" )
9595 log .critical ("%s while getting branch '%s' of %s" , util .http_error (e ), branch_name , str (concerned_object ))
9696 raise
@@ -129,11 +129,10 @@ def refresh(self) -> Branch:
129129 """
130130 try :
131131 data = json .loads (self .get (APIS ["list" ], params = {"project" : self .concerned_object .key }).text )
132- except HTTPError as e :
133- if e .response .status_code == HTTPStatus .NOT_FOUND :
132+ except ( HTTPError , ConnectionError , RequestException ) as e :
133+ if isinstance ( HTTPError , e ) and e .response .status_code == HTTPStatus .NOT_FOUND :
134134 raise exceptions .ObjectNotFound (self .key , f"{ str (self )} not found in SonarQube" )
135- log .critical ("%s while refreshing %s" , util .http_error (e ), str (self ))
136- raise
135+ log .error ("%s while refreshing %s" , util .http_error (e ), str (self ))
137136 for br in data .get ("branches" , []):
138137 if br ["name" ] == self .name :
139138 self ._load (br )
@@ -186,11 +185,11 @@ def delete(self) -> bool:
186185 """
187186 try :
188187 return sq .delete_object (self , APIS ["delete" ], {"branch" : self .name , "project" : self .concerned_object .key }, _OBJECTS )
189- except HTTPError as e :
190- if e .response .status_code == HTTPStatus .BAD_REQUEST :
188+ except ( HTTPError , ConnectionError , RequestException ) as e :
189+ if isinstance ( e , HTTPError ) and e .response .status_code == HTTPStatus .BAD_REQUEST :
191190 log .warning ("Can't delete %s, it's the main branch" , str (self ))
192191 else :
193- log .critical ("%s while deleting %s" , util .http_error (e ), str (self ))
192+ log .error ("%s while deleting %s" , util .http_error (e ), str (self ))
194193 return False
195194
196195 def new_code (self ) -> str :
@@ -203,8 +202,8 @@ def new_code(self) -> str:
203202 elif self ._new_code is None :
204203 try :
205204 data = json .loads (self .get (api = APIS ["get_new_code" ], params = {"project" : self .concerned_object .key }).text )
206- except HTTPError as e :
207- if e .response .status_code == HTTPStatus .NOT_FOUND :
205+ except ( HTTPError , ConnectionError , RequestException ) as e :
206+ if isinstance ( e , HTTPError ) and e .response .status_code == HTTPStatus .NOT_FOUND :
208207 raise exceptions .ObjectNotFound (self .concerned_object .key , f"{ str (self .concerned_object )} not found" )
209208 log .error ("%s while getting new code period of %s" , util .http_error (e ), str (self ))
210209 raise e
@@ -266,8 +265,8 @@ def rename(self, new_name):
266265 log .info ("Renaming main branch of %s from '%s' to '%s'" , str (self .concerned_object ), self .name , new_name )
267266 try :
268267 self .post (APIS ["rename" ], params = {"project" : self .concerned_object .key , "name" : new_name })
269- except HTTPError as e :
270- if e .response .status_code == HTTPStatus .NOT_FOUND :
268+ except ( HTTPError , ConnectionError , RequestException ) as e :
269+ if isinstance ( HTTPError , e ) and e .response .status_code == HTTPStatus .NOT_FOUND :
271270 raise exceptions .ObjectNotFound (self .concerned_object .key , f"str{ self .concerned_object } not found" )
272271 log .error ("%s while renaming %s" , util .http_error (e ), str (self ))
273272 raise
@@ -365,9 +364,7 @@ def audit(self, audit_settings: types.ConfigSettings) -> list[Problem]:
365364 log .debug ("Auditing %s" , str (self ))
366365 try :
367366 return self .__audit_last_analysis (audit_settings ) + self .__audit_zero_loc () + self .__audit_never_analyzed ()
368- except HTTPError as e :
369- if e .response .status_code == HTTPStatus .FORBIDDEN :
370- log .error ("Not enough permission to fully audit %s" , str (self ))
367+ except (HTTPError , RequestException , Exception ) as e :
371368 log .error ("%s while auditing %s, audit skipped" , util .http_error (e ), str (self ))
372369 else :
373370 log .debug ("Branch audit disabled, skipping audit of %s" , str (self ))
0 commit comments