From 0904a855a27deb4b7a79989a425d228d39ab5ffe Mon Sep 17 00:00:00 2001 From: Poeloe <22234727+Poeloe@users.noreply.github.com> Date: Tue, 7 May 2024 17:50:54 +1000 Subject: [PATCH 1/2] Return descriptive error when target tools point to a non-existing file (DIS-2565) --- dissect/target/loaders/raw.py | 7 +++++++ dissect/target/target.py | 6 +++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/dissect/target/loaders/raw.py b/dissect/target/loaders/raw.py index 9b59a1e11..dd161de3f 100644 --- a/dissect/target/loaders/raw.py +++ b/dissect/target/loaders/raw.py @@ -1,6 +1,7 @@ from pathlib import Path from dissect.target import container +from dissect.target.exceptions import TargetError from dissect.target.loader import Loader from dissect.target.target import Target @@ -8,6 +9,12 @@ class RawLoader(Loader): """Load raw container files such as disk images.""" + def __init__(self, path: Path, **kwargs): + if not path.exists(): + raise TargetError(f"Provided path '{path}' does not exist") + + super().__init__(path, **kwargs) + @staticmethod def detect(path: Path) -> bool: return not path.is_dir() diff --git a/dissect/target/target.py b/dissect/target/target.py index ac922310c..692c6760e 100644 --- a/dissect/target/target.py +++ b/dissect/target/target.py @@ -284,7 +284,11 @@ def _find(find_path: Path, parsed_path: Optional[urllib.parse.ParseResult]): try: ldr = loader_cls(sub_entry, parsed_path=parsed_path) except Exception as e: - getlogger(sub_entry).error("Failed to initiate loader: %s", e) + message = "Failed to initiate loader: %s" + if isinstance(e, TargetError) and "does not exist" in str(e): + message = "%s" + + getlogger(sub_entry).error(message, e) getlogger(sub_entry).debug("", exc_info=e) continue From 5e5d1179c2b5827fa81bcdd8c7f51a3fe4e36dc1 Mon Sep 17 00:00:00 2001 From: Poeloe <22234727+Poeloe@users.noreply.github.com> Date: Thu, 9 May 2024 09:33:17 +1000 Subject: [PATCH 2/2] Process review feedback --- dissect/target/exceptions.py | 4 ++++ dissect/target/loaders/raw.py | 4 ++-- dissect/target/target.py | 3 ++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/dissect/target/exceptions.py b/dissect/target/exceptions.py index 22f46a604..aea713290 100644 --- a/dissect/target/exceptions.py +++ b/dissect/target/exceptions.py @@ -114,3 +114,7 @@ class RegistryCorruptError(RegistryError): class ConfigurationParsingError(Error): """An error occurred during configuration parsing.""" + + +class TargetPathNotFoundError(TargetError): + """The path to the target does not exist.""" diff --git a/dissect/target/loaders/raw.py b/dissect/target/loaders/raw.py index dd161de3f..c4c851e01 100644 --- a/dissect/target/loaders/raw.py +++ b/dissect/target/loaders/raw.py @@ -1,7 +1,7 @@ from pathlib import Path from dissect.target import container -from dissect.target.exceptions import TargetError +from dissect.target.exceptions import TargetPathNotFoundError from dissect.target.loader import Loader from dissect.target.target import Target @@ -11,7 +11,7 @@ class RawLoader(Loader): def __init__(self, path: Path, **kwargs): if not path.exists(): - raise TargetError(f"Provided path '{path}' does not exist") + raise TargetPathNotFoundError("Provided target path does not exist") super().__init__(path, **kwargs) diff --git a/dissect/target/target.py b/dissect/target/target.py index 692c6760e..7ef02b1b6 100644 --- a/dissect/target/target.py +++ b/dissect/target/target.py @@ -14,6 +14,7 @@ PluginError, PluginNotFoundError, TargetError, + TargetPathNotFoundError, UnsupportedPluginError, VolumeSystemError, ) @@ -285,7 +286,7 @@ def _find(find_path: Path, parsed_path: Optional[urllib.parse.ParseResult]): ldr = loader_cls(sub_entry, parsed_path=parsed_path) except Exception as e: message = "Failed to initiate loader: %s" - if isinstance(e, TargetError) and "does not exist" in str(e): + if isinstance(e, TargetPathNotFoundError): message = "%s" getlogger(sub_entry).error(message, e)