Skip to content

Commit b88f432

Browse files
authored
Merge pull request #474 from PyFilesystem/fix-patherror
Add missing `exc` argument to `PathError`
2 parents 60b0a63 + eebb4a2 commit b88f432

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1313
- Added FTP over TLS (FTPS) support to FTPFS.
1414
Closes [#437](https://github.com/PyFilesystem/pyfilesystem2/issues/437),
1515
[#449](https://github.com/PyFilesystem/pyfilesystem2/pull/449).
16+
- `PathError` now supports wrapping an exception using the `exc` argument.
17+
Closes [#453](https://github.com/PyFilesystem/pyfilesystem2/issues/453).
1618

1719
### Changed
1820

fs/errors.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,14 @@ class PathError(FSError):
139139

140140
default_message = "path '{path}' is invalid"
141141

142-
def __init__(self, path, msg=None): # noqa: D107
143-
# type: (Text, Optional[Text]) -> None
142+
def __init__(self, path, msg=None, exc=None): # noqa: D107
143+
# type: (Text, Optional[Text], Optional[Exception]) -> None
144144
self.path = path
145+
self.exc = exc
145146
super(PathError, self).__init__(msg=msg)
146147

147148
def __reduce__(self):
148-
return type(self), (self.path, self._msg)
149+
return type(self), (self.path, self._msg, self.exc)
149150

150151

151152
class NoSysPath(PathError):

tests/test_error_tools.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,23 @@
33
import errno
44
import unittest
55

6+
import fs.errors
67
from fs.error_tools import convert_os_errors
7-
from fs import errors as fserrors
88

99

1010
class TestErrorTools(unittest.TestCase):
11-
def assert_convert_os_errors(self):
11+
def test_convert_enoent(self):
12+
exception = OSError(errno.ENOENT, "resource not found")
13+
with self.assertRaises(fs.errors.ResourceNotFound) as ctx:
14+
with convert_os_errors("stat", "/tmp/test"):
15+
raise exception
16+
self.assertEqual(ctx.exception.exc, exception)
17+
self.assertEqual(ctx.exception.path, "/tmp/test")
1218

13-
with self.assertRaises(fserrors.ResourceNotFound):
14-
with convert_os_errors("foo", "test"):
15-
raise OSError(errno.ENOENT)
19+
def test_convert_enametoolong(self):
20+
exception = OSError(errno.ENAMETOOLONG, "File name too long: test")
21+
with self.assertRaises(fs.errors.PathError) as ctx:
22+
with convert_os_errors("stat", "/tmp/test"):
23+
raise exception
24+
self.assertEqual(ctx.exception.exc, exception)
25+
self.assertEqual(ctx.exception.path, "/tmp/test")

0 commit comments

Comments
 (0)