From fe4bbda511384d591d128de60c47e8ec8c101343 Mon Sep 17 00:00:00 2001 From: Erik Schamper <1254028+Schamper@users.noreply.github.com> Date: Fri, 1 Nov 2024 14:58:14 +0100 Subject: [PATCH] Subclass exceptions from standard library exceptions (#18) --- dissect/btrfs/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/btrfs/exceptions.py b/dissect/btrfs/exceptions.py index fe8e760..1b34e2c 100644 --- a/dissect/btrfs/exceptions.py +++ b/dissect/btrfs/exceptions.py @@ -2,15 +2,15 @@ class Error(Exception): pass -class FileNotFoundError(Error): +class FileNotFoundError(Error, FileNotFoundError): pass -class NotAFileError(Error): +class IsADirectoryError(Error, IsADirectoryError): pass -class NotADirectoryError(Error): +class NotADirectoryError(Error, NotADirectoryError): pass diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py new file mode 100644 index 0000000..c8eed63 --- /dev/null +++ b/tests/test_exceptions.py @@ -0,0 +1,19 @@ +import pytest + +from dissect.btrfs 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) + assert isinstance(exc(), std) + + with pytest.raises(std): + raise exc()