Skip to content

Commit 2825247

Browse files
committed
Log all HTTP errors
1 parent ebf0911 commit 2825247

23 files changed

+106
-67
lines changed

cli/findings_export.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ def __get_component_findings(queue: Queue[tuple[object, ConfigSettings]], write_
295295
component.endpoint, component.key, branch=params.get("branch", None), pull_request=params.get("pullRequest", None)
296296
)
297297
except HTTPError as e:
298-
log.critical("Error %s while exporting findings of %s, skipped", str(e), str(component))
298+
log.critical("%s while exporting findings of %s, skipped", util.http_error(e), str(component))
299299
findings_list = {}
300300
write_queue.put([findings_list, False])
301301
else:
@@ -324,7 +324,7 @@ def __get_component_findings(queue: Queue[tuple[object, ConfigSettings]], write_
324324
try:
325325
findings_list = component.get_issues(filters=new_params)
326326
except HTTPError as e:
327-
log.critical("Error %s while exporting issues of %s, skipped", str(e), str(component))
327+
log.critical("%s while exporting issues of %s, skipped", util.http_error(e), str(component))
328328
findings_list = {}
329329
else:
330330
log.debug("Status = %s, Types = %s, Resol = %s, Sev = %s", str(i_statuses), str(i_types), str(i_resols), str(i_sevs))
@@ -334,7 +334,7 @@ def __get_component_findings(queue: Queue[tuple[object, ConfigSettings]], write_
334334
try:
335335
findings_list.update(component.get_hotspots(filters=new_params))
336336
except HTTPError as e:
337-
log.critical("Error %s while exporting hotspots of object key %s, skipped", str(e), str(component))
337+
log.critical("%s while exporting hotspots of object key %s, skipped", util.http_error(e), str(component))
338338
else:
339339
log.debug("Status = %s, Types = %s, Resol = %s, Sev = %s", str(h_statuses), str(h_types), str(h_resols), str(h_sevs))
340340
log.info("Selected types, severities, resolutions or statuses disables issue search")
@@ -352,7 +352,7 @@ def store_findings(components_list: dict[str, object], params: ConfigSettings) -
352352
log.debug("Queue %s task %s put", str(my_queue), str(comp))
353353
my_queue.put((comp, params.copy()))
354354
except HTTPError as e:
355-
log.critical("Error %s while exporting findings of %s, skipped", str(e), str(comp))
355+
log.critical("%s while exporting findings of %s, skipped", util.http_error(e), str(comp))
356356

357357
threads = params.get(options.NBR_THREADS, 4)
358358
for i in range(min(threads, len(components_list))):

cli/loc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def __get_csv_row(o: object, **kwargs) -> tuple[list[str], str]:
5252
try:
5353
loc = o.loc()
5454
except HTTPError as e:
55-
log.warning("HTTP Error %s, LoC export of %s skipped", str(e), str(o))
55+
log.warning("%s, LoC export of %s skipped", util.http_error(e), str(o))
5656
loc = ""
5757
arr = [o.key, loc]
5858
obj_type = type(o).__name__.lower()
@@ -114,7 +114,7 @@ def __get_object_json_data(o: object, **kwargs) -> dict[str, str]:
114114
try:
115115
d["ncloc"] = o.loc()
116116
except HTTPError as e:
117-
log.warning("HTTP Error %s, LoC export of %s skipped", str(e), str(o))
117+
log.warning("%s, LoC export of %s skipped", util.http_error(e), str(o))
118118
if kwargs[options.WITH_NAME]:
119119
d[f"{parent_type}Name"] = o.name
120120
if obj_type in ("branch", "applicationbranch"):

cli/measures_export.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,7 @@ def __last_analysis(component: object) -> str:
5656

5757
def __get_json_measures_history(obj: object, wanted_metrics: types.KeyList) -> dict[str, str]:
5858
"""Returns the measure history of an object (project, branch, application, portfolio)"""
59-
data = {}
60-
try:
61-
data["history"] = obj.get_measures_history(wanted_metrics)
62-
except HTTPError as e:
63-
log.error("HTTP Error %s, measures history export of %s skipped", str(e), str(obj))
64-
return data
59+
return {"history": obj.get_measures_history(wanted_metrics)}
6560

6661

6762
def __get_object_measures(obj: object, wanted_metrics: types.KeyList) -> dict[str, str]:
@@ -289,10 +284,7 @@ def __get_measures(obj: object, wanted_metrics: types.KeyList, hist: bool) -> Un
289284
else:
290285
data.update(__get_object_measures(obj, wanted_metrics))
291286
except HTTPError as e:
292-
if e.response.status_code == HTTPStatus.FORBIDDEN:
293-
log.error("Insufficient permission to retrieve measures of %s, export skipped for this object", str(obj))
294-
else:
295-
log.error("HTTP Error %s while retrieving measures of %s, export skipped for this object", str(e), str(obj))
287+
log.error("%s, measures export skipped for %s", util.http_error(e), str(obj))
296288
return None
297289
return data
298290

sonar/app_branches.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
from sonar.applications import Application as App
3636
from sonar.branches import Branch
37-
from sonar import exceptions, projects
37+
from sonar import exceptions, projects, utilities
3838
import sonar.sqobject as sq
3939

4040
_OBJECTS = {}
@@ -112,6 +112,8 @@ def create(cls, app: App, name: str, project_branches: list[Branch]) -> Applicat
112112
except HTTPError as e:
113113
if e.response.status_code == HTTPStatus.BAD_REQUEST:
114114
raise exceptions.ObjectAlreadyExists(f"app.App {app.key} branch '{name}", e.response.text)
115+
log.critical("%s while creating branch '%s' of '%s'", utilities.http_error(e), name, str(app))
116+
raise
115117
return ApplicationBranch(app=app, name=name, project_branches=project_branches)
116118

117119
@classmethod
@@ -185,6 +187,9 @@ def update(self, name: str, project_branches: list[Branch]) -> bool:
185187
except HTTPError as e:
186188
if e.response.status_code == HTTPStatus.NOT_FOUND:
187189
raise exceptions.ObjectNotFound(str(self), e.response.text)
190+
log.critical("%s while updating '%s'", utilities.http_error(e), str(self))
191+
raise
192+
188193
self.name = name
189194
self._project_branches = project_branches
190195
return ok

sonar/applications.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ def get_object(cls, endpoint: pf.Platform, key: str) -> Application:
9393
except HTTPError as e:
9494
if e.response.status_code == HTTPStatus.NOT_FOUND:
9595
raise exceptions.ObjectNotFound(key, f"Application key '{key}' not found")
96+
log.critical("%s while getting app key '%s'", util.http_error(e), key)
97+
raise
9698
return cls.load(endpoint, data)
9799

98100
@classmethod
@@ -134,6 +136,8 @@ def create(cls, endpoint: pf.Platform, key: str, name: str) -> Application:
134136
except HTTPError as e:
135137
if e.response.status_code == HTTPStatus.BAD_REQUEST:
136138
raise exceptions.ObjectAlreadyExists(key, e.response.text)
139+
log.critical("%s while creating app key '%s'", util.http_error(e), key)
140+
raise
137141
return Application(endpoint, key, name)
138142

139143
def refresh(self) -> None:
@@ -150,6 +154,7 @@ def refresh(self) -> None:
150154
if e.response.status_code == HTTPStatus.NOT_FOUND:
151155
_OBJECTS.pop(self.uuid(), None)
152156
raise exceptions.ObjectNotFound(self.key, f"{str(self)} not found")
157+
log.critical("%s while refreshing %s", util.http_error(e), str(self))
153158
raise
154159

155160
def __str__(self) -> str:
@@ -388,6 +393,7 @@ def add_projects(self, project_list: list[str]) -> bool:
388393
log.warning("Project '%s' not found, can't be added to %s", proj, self)
389394
ok = False
390395
else:
396+
log.critical("%s while adding projects to %s", util.http_error(e), str(self))
391397
raise
392398
return ok
393399

sonar/branches.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ def get_object(cls, concerned_object: projects.Project, branch_name: str) -> Bra
9292
except HTTPError as e:
9393
if 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.http_error(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)
@@ -130,6 +132,8 @@ def refresh(self) -> Branch:
130132
except HTTPError as e:
131133
if e.response.status_code == HTTPStatus.NOT_FOUND:
132134
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
133137
for br in data.get("branches", []):
134138
if br["name"] == self.name:
135139
self._load(br)
@@ -173,7 +177,7 @@ def is_main(self):
173177
self.refresh()
174178
return self._is_main
175179

176-
def delete(self):
180+
def delete(self) -> bool:
177181
"""Deletes a branch
178182
179183
:raises ObjectNotFound: Branch not found for deletion
@@ -185,6 +189,8 @@ def delete(self):
185189
except HTTPError as e:
186190
if e.response.status_code == HTTPStatus.BAD_REQUEST:
187191
log.warning("Can't delete %s, it's the main branch", str(self))
192+
else:
193+
log.critical("%s while deleting %s", util.http_error(e), str(self))
188194
return False
189195

190196
def new_code(self) -> str:
@@ -200,8 +206,7 @@ def new_code(self) -> str:
200206
except HTTPError as e:
201207
if e.response.status_code == HTTPStatus.NOT_FOUND:
202208
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)})
209+
log.error("%s while getting new code period of %s", util.http_error(e), str(self))
205210
raise e
206211
for b in data["newCodePeriods"]:
207212
new_code = settings.new_code_to_string(b)
@@ -264,6 +269,8 @@ def rename(self, new_name):
264269
except HTTPError as e:
265270
if e.response.status_code == HTTPStatus.NOT_FOUND:
266271
raise exceptions.ObjectNotFound(self.concerned_object.key, f"str{self.concerned_object} not found")
272+
log.error("%s while renaming %s", util.http_error(e), str(self))
273+
raise
267274
_OBJECTS.pop(self.uuid(), None)
268275
self.name = new_name
269276
_OBJECTS[self.uuid()] = self
@@ -361,8 +368,7 @@ def audit(self, audit_settings: types.ConfigSettings) -> list[Problem]:
361368
except HTTPError as e:
362369
if e.response.status_code == HTTPStatus.FORBIDDEN:
363370
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))
371+
log.error("%s while auditing %s, audit skipped", util.http_error(e), str(self))
366372
else:
367373
log.debug("Branch audit disabled, skipping audit of %s", str(self))
368374
return []

sonar/devops.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ def create(cls, endpoint: platform.Platform, key: str, plt_type: str, url_or_wor
113113
if e.response.status_code == HTTPStatus.BAD_REQUEST and endpoint.edition() in ("community", "developer"):
114114
log.warning("Can't set DevOps platform '%s', don't you have more that 1 of that type?", key)
115115
raise exceptions.UnsupportedOperation(f"Can't set DevOps platform '{key}', don't you have more that 1 of that type?")
116+
log.error("%s while creating devops platform %s/%s/%s", util.http_error(e), key, plt_type, url_or_workspace)
116117
raise
117118
o = DevopsPlatform(endpoint=endpoint, key=key, platform_type=plt_type)
118119
o.refresh()

sonar/hotspots.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ def search(endpoint: pf.Platform, filters: types.ApiParams = None) -> dict[str,
407407
log.warning("No hotspots found with search params %s", str(inline_filters))
408408
nbr_hotspots = 0
409409
return {}
410+
log.error("%s while searching hotspots", util.http_error(e))
410411
raise e
411412
nbr_pages = util.nbr_pages(data)
412413
log.debug("Number of hotspots: %d - Page: %d/%d", nbr_hotspots, inline_filters["p"], nbr_pages)

sonar/issues.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ def __search_thread(queue: Queue) -> None:
733733
issue_list[i["key"]] = get_object(endpoint=endpoint, key=i["key"], data=i)
734734
log.debug("Added %d issues in threaded search page %d", len(data["issues"]), page)
735735
except HTTPError as e:
736-
log.critical("HTTP Error while searching issues, search may be incomplete: %s", str(e))
736+
log.error("%s while searching issues, search may be incomplete", util.http_error(e))
737737
queue.task_done()
738738

739739

@@ -794,6 +794,7 @@ def search(endpoint: pf.Platform, params: ApiParams = None, raise_error: bool =
794794
worker.setDaemon(True)
795795
worker.start()
796796
q.join()
797+
log.debug("Issue search for %s completed with %d issues", str(params), len(issue_list))
797798
return issue_list
798799

799800

@@ -849,13 +850,16 @@ def count_by_rule(endpoint: pf.Platform, **kwargs) -> dict[str, int]:
849850
rulecount = {}
850851
for i in range(nbr_slices):
851852
params["rules"] = ",".join(ruleset[i * SLICE_SIZE : min((i + 1) * SLICE_SIZE - 1, len(ruleset))])
852-
data = json.loads(endpoint.get(Issue.SEARCH_API, params=params).text)["facets"][0]["values"]
853-
for d in data:
854-
if d["val"] not in ruleset:
855-
continue
856-
if d["val"] not in rulecount:
857-
rulecount[d["val"]] = 0
858-
rulecount[d["val"]] += d["count"]
853+
try:
854+
data = json.loads(endpoint.get(Issue.SEARCH_API, params=params).text)["facets"][0]["values"]
855+
for d in data:
856+
if d["val"] not in ruleset:
857+
continue
858+
if d["val"] not in rulecount:
859+
rulecount[d["val"]] = 0
860+
rulecount[d["val"]] += d["count"]
861+
except HTTPError as e:
862+
log.warning("%s while counting issues per rule, count may be incomplete", util.http_error(e))
859863
return rulecount
860864

861865

sonar/measures.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ def get(concerned_object: object, metrics_list: KeyList, **kwargs) -> dict[str,
127127
except HTTPError as e:
128128
if e.response.status_code == HTTPStatus.NOT_FOUND:
129129
raise exceptions.ObjectNotFound(concerned_object.key, f"{str(concerned_object)} not found")
130+
log.error("%s while getting measures %s of %s", util.http_error(e), str(metrics_list), str(concerned_object))
130131
raise e
131132
m_dict = {m: None for m in metrics_list}
132133
for m in data["component"]["measures"]:
@@ -157,6 +158,7 @@ def get_history(concerned_object: object, metrics_list: KeyList, **kwargs) -> li
157158
except HTTPError as e:
158159
if e.response.status_code == HTTPStatus.NOT_FOUND:
159160
raise exceptions.ObjectNotFound(concerned_object.key, f"{str(concerned_object)} not found")
161+
log.error("%s while getting measures %s history of %s", util.http_error(e), str(metrics_list), str(concerned_object))
160162
raise e
161163
res_list = []
162164
# last_metric, last_date = "", ""

0 commit comments

Comments
 (0)