How do I add error handling to my NetworkManager library in python using sdbus?

59 views Asked by At

I have the following library that talks to NetworkManager using the DBus in python.

here's the relevant code for connecting to a network:

    def add_connection(self, ssid, passwd, autconnect = True, connect=True):
        """ Adds a connection to networkmanager and system
        SSID: the network SSID
        passwd: Password"""


        #Generate properties
        properties: NetworkManagerConnectionProperties = {
            "connection": {
                "id": ("s", ssid),
                "uuid": ("s", str(uuid4())),
                "type": ("s", "802-11-wireless"),
                "autoconnect": ("b", autconnect),
                "interface-name": ("s", self.wlan_device_name)
            },
            "802-11-wireless": {
                "mode": ("s", "infrastructure"),
                "security": ("s", "802-11-wireless-security"),
                "ssid": ("ay", ssid.encode("utf-8")),
            },
            "802-11-wireless-security": {
                "key-mgmt": ("s", "wpa-psk"),
                "auth-alg": ("s", "open"),
                "psk": ("s", passwd),
            },
            "ipv4": {"method": ("s", "auto")},
            "ipv6": {"method": ("s", "auto")},
        }

        nmSettings = NetworkManagerSettings()
        try:
            nmSettings.add_connection(properties)
        except:
            return "Password type is incorrect."
        
        if(connect):
            return self.connect(ssid)
        

    def connect(self, ssid):
        connection = NetworkManagerSettings(self.system_bus).get_connections_by_id(ssid)

        if len(connection) == 0:
            return "Error, network not found"
        
        self.nm.activate_connection(connection[0])

The function works fine, except in cases where the user enters the incorrect password there's no way of knowing and if there's any other errors while connecting I can't find any information about it.

PS: the question was asked here but never got an answer

I've tried using the device.state_reason and state to see what the devices are doing during a connection, but there's no useful info.. (it goes to disconnecting, connecting, disconnecting, connected) with it reverting to it's other known valid network after it fails.

how do I make my code be able to handle and report issues/errors? (this is used by a python GUI, but that part works just fine)

0

There are 0 answers