From ea275e929f93b474fc703756bfaf45f9c052056e Mon Sep 17 00:00:00 2001 From: Schamper <1254028+Schamper@users.noreply.github.com> Date: Wed, 30 Oct 2024 17:11:12 +0100 Subject: [PATCH] Make filesystem exceptions subclass from standard library exceptions --- dissect/target/exceptions.py | 6 +++--- tests/test_exceptions.py | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 tests/test_exceptions.py diff --git a/dissect/target/exceptions.py b/dissect/target/exceptions.py index aea713290..5f52633cc 100644 --- a/dissect/target/exceptions.py +++ b/dissect/target/exceptions.py @@ -72,15 +72,15 @@ class PluginNotFoundError(PluginError): """Plugin cannot be found.""" -class FileNotFoundError(FilesystemError): +class FileNotFoundError(FilesystemError, FileNotFoundError): """The requested path could not be found.""" -class IsADirectoryError(FilesystemError): +class IsADirectoryError(FilesystemError, IsADirectoryError): """The entry is a directory.""" -class NotADirectoryError(FilesystemError): +class NotADirectoryError(FilesystemError, NotADirectoryError): """The entry is not a directory.""" diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py new file mode 100644 index 000000000..29e049430 --- /dev/null +++ b/tests/test_exceptions.py @@ -0,0 +1,19 @@ +import pytest + +from dissect.target import exceptions + + +@pytest.mark.parametrize( + "exc, std", + [ + (exceptions.FileNotFoundError, FileNotFoundError), + (exceptions.IsADirectoryError, IsADirectoryError), + (exceptions.NotADirectoryError, NotADirectoryError), + ], +) +def test_filesystem_error_subclass(exc: exceptions.Error, std: Exception) -> None: + assert issubclass(exc, (std, exceptions.FilesystemError)) + assert isinstance(exc(), (std, exceptions.FilesystemError)) + + with pytest.raises(std): + raise exc()