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,9 +89,11 @@ 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 ( 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" )
95+ log .critical ("%s while getting branch '%s' of %s" , util .error_msg (e ), branch_name , str (concerned_object ))
96+ raise
9597 for br in data .get ("branches" , []):
9698 if br ["name" ] == branch_name :
9799 return cls .load (concerned_object , branch_name , br )
@@ -127,9 +129,10 @@ def refresh(self) -> Branch:
127129 """
128130 try :
129131 data = json .loads (self .get (APIS ["list" ], params = {"project" : self .concerned_object .key }).text )
130- except HTTPError as e :
131- if e .response .status_code == HTTPStatus .NOT_FOUND :
132+ except ( ConnectionError , RequestException ) as e :
133+ if isinstance ( HTTPError , e ) and e .response .status_code == HTTPStatus .NOT_FOUND :
132134 raise exceptions .ObjectNotFound (self .key , f"{ str (self )} not found in SonarQube" )
135+ log .error ("%s while refreshing %s" , util .error_msg (e ), str (self ))
133136 for br in data .get ("branches" , []):
134137 if br ["name" ] == self .name :
135138 self ._load (br )
@@ -173,7 +176,7 @@ def is_main(self):
173176 self .refresh ()
174177 return self ._is_main
175178
176- def delete (self ):
179+ def delete (self ) -> bool :
177180 """Deletes a branch
178181
179182 :raises ObjectNotFound: Branch not found for deletion
@@ -182,9 +185,11 @@ def delete(self):
182185 """
183186 try :
184187 return sq .delete_object (self , APIS ["delete" ], {"branch" : self .name , "project" : self .concerned_object .key }, _OBJECTS )
185- except HTTPError as e :
186- if e .response .status_code == HTTPStatus .BAD_REQUEST :
188+ except ( ConnectionError , RequestException ) as e :
189+ if isinstance ( e , HTTPError ) and e .response .status_code == HTTPStatus .BAD_REQUEST :
187190 log .warning ("Can't delete %s, it's the main branch" , str (self ))
191+ else :
192+ log .error ("%s while deleting %s" , util .error_msg (e ), str (self ))
188193 return False
189194
190195 def new_code (self ) -> str :
@@ -197,11 +202,10 @@ def new_code(self) -> str:
197202 elif self ._new_code is None :
198203 try :
199204 data = json .loads (self .get (api = APIS ["get_new_code" ], params = {"project" : self .concerned_object .key }).text )
200- except HTTPError as e :
201- if e .response .status_code == HTTPStatus .NOT_FOUND :
205+ except ( ConnectionError , RequestException ) as e :
206+ if isinstance ( e , HTTPError ) and e .response .status_code == HTTPStatus .NOT_FOUND :
202207 raise exceptions .ObjectNotFound (self .concerned_object .key , f"{ str (self .concerned_object )} not found" )
203- if e .response .status_code == HTTPStatus .FORBIDDEN :
204- log .error ("Error 403 when getting new code period of %s" , {str (self )})
208+ log .error ("%s while getting new code period of %s" , util .error_msg (e ), str (self ))
205209 raise e
206210 for b in data ["newCodePeriods" ]:
207211 new_code = settings .new_code_to_string (b )
@@ -261,9 +265,11 @@ def rename(self, new_name):
261265 log .info ("Renaming main branch of %s from '%s' to '%s'" , str (self .concerned_object ), self .name , new_name )
262266 try :
263267 self .post (APIS ["rename" ], params = {"project" : self .concerned_object .key , "name" : new_name })
264- except HTTPError as e :
265- if e .response .status_code == HTTPStatus .NOT_FOUND :
268+ except ( ConnectionError , RequestException ) as e :
269+ if isinstance ( HTTPError , e ) and e .response .status_code == HTTPStatus .NOT_FOUND :
266270 raise exceptions .ObjectNotFound (self .concerned_object .key , f"str{ self .concerned_object } not found" )
271+ log .error ("%s while renaming %s" , util .error_msg (e ), str (self ))
272+ raise
267273 _OBJECTS .pop (self .uuid (), None )
268274 self .name = new_name
269275 _OBJECTS [self .uuid ()] = self
@@ -358,11 +364,8 @@ def audit(self, audit_settings: types.ConfigSettings) -> list[Problem]:
358364 log .debug ("Auditing %s" , str (self ))
359365 try :
360366 return self .__audit_last_analysis (audit_settings ) + self .__audit_zero_loc () + self .__audit_never_analyzed ()
361- except HTTPError as e :
362- if e .response .status_code == HTTPStatus .FORBIDDEN :
363- log .error ("Not enough permission to fully audit %s" , str (self ))
364- else :
365- log .error ("HTTP error %s while auditing %s" , str (e ), str (self ))
367+ except Exception as e :
368+ log .error ("%s while auditing %s, audit skipped" , util .error_msg (e ), str (self ))
366369 else :
367370 log .debug ("Branch audit disabled, skipping audit of %s" , str (self ))
368371 return []
0 commit comments