How to correctly catch an exception raised by a module?
Question: In a python project I use a module [python-
mpd2](https://github.com/Mic92/python-mpd2) that connects to an (mpd.)server.
The server closes the connection after one minute. Most methods provided by
the module would then result in an `mpd.ConnectionError`.

I try to build a wrapper class that tries to execute the method but reconnects
to the server in case of a previous disconnect.

What I have is this:

    
    
    from mpd import MPDClient, MPDError
    
    class MPDProxy:
        def __init__(self, host="localhost", port=6600, timeout=10):
            self.client = MPDClient()
            self.host = host
            self.port = port
    
            self.client.timeout = timeout
            self.connect(host, port)
    
        def __getattr__(self, name):
            return self._call_with_reconnect(getattr(self.client, name))
    
        def connect(self, host, port):
            self.client.connect(host, port)
            self.client.consume(1) # when we call self.client.next() the previous stream is deleted from the playlist
            if len(self.client.playlist()) > 1:
                cur =  (self.client.playlist()[0][6:])
                self.client.clear()
                self.add(cur)
    
        def _call_with_reconnect(self, func):
            def wrapper(*args, **kwargs):
                try:
                    return func(*args, **kwargs)
                except ConnectionError:
                    self.connect(self.host, self.port)
                    return func(*args, **kwargs)
            return wrapper
    
    mpd_proxy = MPDProxy()
    

However, the `ConnectionError` would not be caught.

    
    
    >>> from MPDProxy import mpd_proxy
    >>> mpd_proxy.play()
    >>> mpd_proxy.stop()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "./MPDProxy.py", line 26, in wrapper
        func(*args, **kwargs)
      File "/usr/local/lib/python3.3/dist-packages/mpd.py", line 588, in decorator
        return wrapper(self, name, args, bound_decorator(self, returnValue))
      File "/usr/local/lib/python3.3/dist-packages/mpd.py", line 229, in _execute
        return retval()
      File "/usr/local/lib/python3.3/dist-packages/mpd.py", line 583, in decorator
        return function(self, *args, **kwargs)
      File "/usr/local/lib/python3.3/dist-packages/mpd.py", line 352, in _fetch_nothing
        line = self._read_line()
      File "/usr/local/lib/python3.3/dist-packages/mpd.py", line 260, in _read_line
        raise ConnectionError("Connection lost while reading line")
    mpd.ConnectionError: Connection lost while reading line
    

How would I correctly catch the ConnectionError?


Answer: you need something like the following:

    
    
     def connect(self):
        try:
            self.client.connect(self._host, self._port)
        # Catch socket errors
        except IOError as err:
            errno, strerror = err
            raise PollerError("Could not connect to '%s': %s" %
                              (self._host, strerror))
    
        # Catch all other possible errors
        # ConnectionError and ProtocolError are always fatal.  Others may not
        # be, but we don't know how to handle them here, so treat them as if
        # they are instead of ignoring them.
        except MPDError as e:
            raise PollerError("Could not connect to '%s': %s" %
                              (self._host, e))
    

For more info on this look at the examples under
[Documentation](https://github.com/Mic92/python-mpd2#documentation) Also,
according to your error message the correct error to catch for
`ConnectionError` is `mdp.ConnectionError`. ConnectionError is an exception
defined by mdp.

