From 55f1ddfd467fe5ae8bdde9794ab6fbd30fe85682 Mon Sep 17 00:00:00 2001 From: Anastasiia Pnevskaia Date: Tue, 29 Oct 2024 10:54:03 +0100 Subject: [PATCH] Silence error messages in Telemetry. --- src/backend/backend_ga.py | 3 ++- src/backend/backend_ga4.py | 5 ++++- src/main.py | 1 + src/utils/opt_in_checker.py | 37 ++++++++++++++++++++++++------------- 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/backend/backend_ga.py b/src/backend/backend_ga.py index 5d98f85..aea4fef 100644 --- a/src/backend/backend_ga.py +++ b/src/backend/backend_ga.py @@ -38,7 +38,8 @@ def send(self, message: Message): if self.backend_url.lower().startswith('http'): req = request.Request(self.backend_url, data=data) else: - raise ValueError("Incorrect backend URL.") + log.warning("Incorrect backend URL.") + return request.urlopen(req) #nosec except Exception as err: diff --git a/src/backend/backend_ga4.py b/src/backend/backend_ga4.py index 4f12ae5..72a1b4a 100644 --- a/src/backend/backend_ga4.py +++ b/src/backend/backend_ga4.py @@ -3,6 +3,8 @@ import json import uuid +import logging as log + from copy import copy from urllib import request @@ -39,7 +41,8 @@ def send(self, message: dict): if self.backend_url.lower().startswith('http'): req = request.Request(self.backend_url, data=data) else: - raise ValueError("Incorrect backend URL.") + log.warning("Incorrect backend URL.") + return request.urlopen(req) # nosec except Exception as err: diff --git a/src/main.py b/src/main.py index 2a2d2a4..9285f56 100644 --- a/src/main.py +++ b/src/main.py @@ -248,6 +248,7 @@ def _update_opt_in_status(tid: str, new_opt_in_status: bool): else: updated = opt_in_checker.update_result(ConsentCheckResult.DECLINED) if not updated: + print("Could not update the consent file. No telemetry will be sent.") return telemetry = Telemetry(tid=tid, app_name=app_name, app_version=app_version, backend='ga4') diff --git a/src/utils/opt_in_checker.py b/src/utils/opt_in_checker.py index 6cd44f9..8f464f7 100644 --- a/src/utils/opt_in_checker.py +++ b/src/utils/opt_in_checker.py @@ -97,11 +97,13 @@ def consent_file_base_dir(): dir_to_check = Path.home() if dir_to_check is None: - raise Exception('Failed to find location of the openvino_telemetry file.') + log.warning('Failed to find location of the openvino_telemetry file.') + return None consent_base_dir = os.path.expandvars(dir_to_check) if not os.path.isdir(consent_base_dir): - raise Exception('Failed to find location of the openvino_telemetry file.') + log.warning('Failed to find location of the openvino_telemetry file.') + return None return consent_base_dir @@ -116,7 +118,8 @@ def consent_file_subdirectory(): return 'Intel Corporation' elif platform in ['Linux', 'Darwin']: return 'intel' - raise Exception('Failed to find location of the openvino_telemetry file.') + log.warning('Failed to find location of the openvino_telemetry file.') + return None def consent_file(self): """ @@ -144,6 +147,9 @@ def create_or_check_consent_dir(self): :return: True if the directory is created and writable, otherwise False """ base_dir = self.consent_file_base_dir() + consent_file_subdirectory = self.consent_file_subdirectory() + if self.consent_file_base_dir() is None or consent_file_subdirectory is None: + return False base_is_dir = os.path.isdir(base_dir) base_dir_exists = os.path.exists(base_dir) base_w_access = os.access(base_dir, os.W_OK) @@ -155,33 +161,33 @@ def create_or_check_consent_dir(self): "Please allow write access to the following directory: {}".format(base_dir)) return False - consect_file_dir = os.path.join(self.consent_file_base_dir(), self.consent_file_subdirectory()) - consent_file_is_dir = os.path.isdir(consect_file_dir) - consent_file_dir_exists = os.path.exists(consect_file_dir) + consent_file_dir = os.path.join(self.consent_file_base_dir(), consent_file_subdirectory) + consent_file_is_dir = os.path.isdir(consent_file_dir) + consent_file_dir_exists = os.path.exists(consent_file_dir) # If consent path exists and it is not directory, we try to remove it if consent_file_dir_exists and not consent_file_is_dir: try: - os.remove(consect_file_dir) + os.remove(consent_file_dir) except: - log.warning("Unable to create directory for openvino_telemetry file, as {} is invalid directory.".format(consect_file_dir)) + log.warning("Unable to create directory for openvino_telemetry file, as {} is invalid directory.".format(consent_file_dir)) return False - if not os.path.exists(consect_file_dir): + if not os.path.exists(consent_file_dir): try: - os.mkdir(consect_file_dir) + os.mkdir(consent_file_dir) # check that directory is created - if not os.path.exists(consect_file_dir): + if not os.path.exists(consent_file_dir): return False except Exception as e: log.warning("Failed to create directory for openvino_telemetry file: {}".format(str(e))) return False - consent_file_w_access = os.access(consect_file_dir, os.W_OK) + consent_file_w_access = os.access(consent_file_dir, os.W_OK) if not consent_file_w_access: log.warning("Failed to create openvino_telemetry file. " - "Please allow write access to the following directory: {}".format(consect_file_dir)) + "Please allow write access to the following directory: {}".format(consent_file_dir)) return False return True @@ -191,6 +197,8 @@ def update_result(self, result: ConsentCheckResult): :param result: opt-in dialog result. :return: False if the consent file is not writable, otherwise True """ + if self.consent_file_base_dir() is None or self.consent_file_subdirectory() is None: + return False if not os.path.exists(self.consent_file()): if not self.create_new_consent_file(): return False @@ -297,6 +305,9 @@ def check(self, enable_opt_in_dialog, disable_in_ci=False): Checks if user has accepted the collection of the information by checking the consent file. :return: consent check result """ + if self.consent_file_base_dir() is None or self.consent_file_subdirectory() is None: + return ConsentCheckResult.DECLINED + if disable_in_ci and self._run_in_ci(): return ConsentCheckResult.DECLINED