Repository: PyFilesystem/pyfilesystem2

Issue:
FTPFS: EOFError in listdir
Hello,

I am new to pyfilesystem, so please excuse if i might have missed something. From what i can understand from the documentation pyfilesystem2 translates exceptions from the underlying filesystem into a common set of exceptions.

There seems to be a corner case for ftpfs where it might raise EOFError in listdir if the server forcefully closes the connection during the operation. This exception seems to be coming from the underlying ftplib.

Discussion:
coveralls: 
[![Coverage Status](https://coveralls.io/builds/24817324/badge)](https://coveralls.io/builds/24817324)

Coverage decreased (-0.5%) to 99.078% when pulling **aea8d46794696856b25cd1c4809ce20389c71ce4 on dargueta:ftp-eoferror** into **37b46b4a991a124f4d8e199e111ee21f00c2213b on PyFilesystem:master**.

willmcgugan: Thanks

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b36f9a9..d92e0ec 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
 
 - `MemFS` now immediately releases all memory it holds when `close()` is called,
   rather than when it gets garbage collected. Closes [issue #308](https://github.com/PyFilesystem/pyfilesystem2/issues/308).
+- `FTPFS` now translates `EOFError` into `RemoteConnectionError`. Closes [#292](https://github.com/PyFilesystem/pyfilesystem2/issues/292)
 
 ## [2.4.8] - 2019-06-12
 
diff --git a/fs/ftpfs.py b/fs/ftpfs.py
index 8e295da..21e98a1 100644
--- a/fs/ftpfs.py
+++ b/fs/ftpfs.py
@@ -77,6 +77,8 @@ def ftp_errors(fs, path=None):
         raise errors.RemoteConnectionError(
             msg="unable to connect to {}".format(fs.host)
         )
+    except EOFError:
+        raise errors.RemoteConnectionError(msg="lost connection to {}".format(fs.host))
     except error_temp as error:
         if path is not None:
             raise errors.ResourceError(


Test patch:
diff --git a/tests/test_ftpfs.py b/tests/test_ftpfs.py
index df6c97a..19ce22e 100644
--- a/tests/test_ftpfs.py
+++ b/tests/test_ftpfs.py
@@ -4,7 +4,6 @@ from __future__ import print_function
 from __future__ import unicode_literals
 
 import socket
-import ftplib
 import os
 import platform
 import shutil
@@ -114,6 +113,20 @@ class TestFTPErrors(unittest.TestCase):
             with ftp_errors(mem_fs):
                 raise error_perm("999 foo")
 
+    def test_manager_with_host(self):
+        mem_fs = open_fs("mem://")
+        mem_fs.host = "ftp.example.com"
+
+        with self.assertRaises(errors.RemoteConnectionError) as err_info:
+            with ftp_errors(mem_fs):
+                raise EOFError
+        self.assertEqual(str(err_info.exception), "lost connection to ftp.example.com")
+
+        with self.assertRaises(errors.RemoteConnectionError) as err_info:
+            with ftp_errors(mem_fs):
+                raise socket.error
+        self.assertEqual(str(err_info.exception), "unable to connect to ftp.example.com")
+
 
 @attr("slow")
 class TestFTPFS(FSTestCases, unittest.TestCase):
@@ -167,7 +180,7 @@ class TestFTPFS(FSTestCases, unittest.TestCase):
 
     def test_ftp_url(self):
         self.assertEqual(self.fs.ftp_url, "ftp://{}:{}@{}:{}".format(self.user, self.pasw, self.server.host, self.server.port))
-        
+
     def test_geturl(self):
         self.fs.makedir("foo")
         self.fs.create("bar")
@@ -251,7 +264,7 @@ class TestFTPFSNoMLSD(TestFTPFS):
     def test_features(self):
         pass
 
-    
+
 @attr("slow")
 class TestAnonFTPFS(FSTestCases, unittest.TestCase):