diff --git a/src/wifi_connection.py b/src/wifi_connection.py index 8d3ce7d..f08374e 100644 --- a/src/wifi_connection.py +++ b/src/wifi_connection.py @@ -36,6 +36,8 @@ class UnsupportedSecurityError(WiFiConnectionError): class NetworkRestoreError(WiFiConnectionError): pass +class NetworkCleanupError(WiFiConnectionError): + pass @dataclass(frozen=True) @@ -495,22 +497,45 @@ class WiFiConnectionService: *arguments ) - output = self._nmcli( - "--get-values", - "connection.uuid", - "connection", - "show", - profile_name - ) + try: - if not re.fullmatch( - r"[0-9a-fA-F-]{36}", - output - ): - raise WiFiConnectionError( - "NetworkManager created a profile but returned an invalid UUID." + output = self._nmcli( + "--get-values", + "connection.uuid", + "connection", + "show", + profile_name ) + if not re.fullmatch( + r"[0-9a-fA-F-]{36}", + output + ): + raise WiFiConnectionError( + "NetworkManager created a profile but returned an invalid UUID." + ) + + except Exception as error: + + try: + + self._nmcli( + "connection", + "delete", + "id", + profile_name + ) + + except NetworkCommandError as cleanup_error: + + raise WiFiConnectionError( + "NetworkManager created a temporary profile, but its UUID " + "could not be obtained and the profile could not be deleted: " + f"{cleanup_error}" + ) from error + + raise + return output @staticmethod @@ -644,17 +669,12 @@ class WiFiConnectionService: if not profile_uuid: return - try: - - self._nmcli( - "connection", - "delete", - "uuid", - profile_uuid - ) - - except NetworkCommandError: - pass + self._nmcli( + "connection", + "delete", + "uuid", + profile_uuid + ) def connect( self, @@ -682,11 +702,21 @@ class WiFiConnectionService: password ) - except Exception: + except Exception as connection_error: - self._delete_profile( - profile_uuid - ) + try: + + self._delete_profile( + profile_uuid + ) + + except NetworkCommandError as cleanup_error: + + raise NetworkCleanupError( + "Wi-Fi connection failed: " + f"{connection_error}. The temporary profile could not be deleted: " + f"{cleanup_error}" + ) from connection_error raise @@ -729,6 +759,7 @@ class WiFiConnectionService: success = False error_message = None restore_error = None + cleanup_error = None try: @@ -781,8 +812,12 @@ class WiFiConnectionService: profile_uuid ) - except NetworkCommandError: - pass + except NetworkCommandError as error: + + if success: + restore_error = str( + error + ) except NetworkCommandError as error: @@ -790,12 +825,26 @@ class WiFiConnectionService: error ) - finally: + try: self._delete_profile( profile_uuid ) + except NetworkCommandError as error: + + cleanup_error = str( + error + ) + + if restore_error and cleanup_error: + + raise NetworkRestoreError( + "The Wi-Fi verification attempt finished, but the previous connection " + f"could not be restored: {restore_error}. " + f"The temporary profile could not be deleted: {cleanup_error}" + ) + if restore_error: raise NetworkRestoreError( @@ -803,10 +852,23 @@ class WiFiConnectionService: f"could not be restored: {restore_error}" ) + if cleanup_error: + + verification_error = ( + f"Password verification failed: {error_message}. " + if error_message else "" + ) + + raise NetworkCleanupError( + f"{verification_error}" + "The temporary Wi-Fi profile could not be deleted: " + f"{cleanup_error}" + ) + return { "success": success, "interface": interface, "bssid": access_point.bssid, "ssid": access_point.ssid, "error": error_message - } + } \ No newline at end of file