Skip to content

Commit bd595f7

Browse files
Add a custom exception when the model path is not correct
Signed-off-by: Idlir Shkurti <[email protected]>
1 parent 8f2eb4a commit bd595f7

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/frequenz/sdk/ml/_model_manager.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ class _Model(Generic[T]):
2828
path: Path
2929

3030

31+
class ModelNotFoundError(Exception):
32+
"""Exception raised when a model is not found."""
33+
34+
def __init__(self, key: str) -> None:
35+
"""Initialize the exception with the specified model key.
36+
37+
Args:
38+
key: The key of the model that was not found.
39+
"""
40+
super().__init__(f"Model with key '{key}' is not found.")
41+
42+
3143
class ModelManager(BackgroundService, Generic[T]):
3244
"""Load, update, monitor and retrieve machine learning models."""
3345

@@ -59,12 +71,13 @@ def _load(path: Path) -> T:
5971
T: The loaded model data.
6072
6173
Raises:
62-
FileNotFoundError: If the model file does not exist.
74+
ModelNotFoundError: If the model file does not exist.
6375
"""
64-
if not path.exists():
65-
raise FileNotFoundError(f"The model path {path} does not exist.")
66-
with path.open("rb") as file:
67-
return cast(T, pickle.load(file))
76+
try:
77+
with path.open("rb") as file:
78+
return cast(T, pickle.load(file))
79+
except FileNotFoundError as exc:
80+
raise ModelNotFoundError(str(path)) from exc
6881

6982
@override
7083
def start(self) -> None:

0 commit comments

Comments
 (0)