Coverage for melissa/server/exceptions.py: 66%
32 statements
« prev ^ index » next coverage.py v7.10.1, created at 2025-11-03 09:52 +0100
« prev ^ index » next coverage.py v7.10.1, created at 2025-11-03 09:52 +0100
1import queue
2from threading import current_thread
4__EXCEPTION_QUEUE: queue.Queue = queue.Queue()
7class MelissaError(Exception):
8 """Base class for melissa errors."""
9 def __init__(self, message=None):
10 if message is None:
11 message = "An error occurred in the application."
12 super().__init__(message)
15class FatalError(MelissaError):
16 """Base class for fatal errors.
17 If fault-tolerance is enabled, the study will raise this error
18 and should be stopped manually.
19 """
20 def __init__(self, message=None):
21 extra_suffix = (
22 "A fatal error has occurred. "
23 "If fault-tolerance is enabled, the study will raise this error again. "
24 "Terminate the study manually."
25 )
26 message = message + "\n" + extra_suffix if message is not None else extra_suffix
27 super().__init__(message)
30class FaultToleranceError(FatalError):
31 """Errors coming from fault-tolerance.
32 Raised when fault-tolerance is off or at the end
33 of total restart attempts."""
36class ConfigurationFileError(FatalError):
37 """Errors from the configuration file."""
40class InitialConnectionError(FatalError):
41 """Errors coming while connecting to the launcher."""
44class UnsupportedProtocol(InitialConnectionError):
45 """Protocol Exception."""
48class ClientError(MelissaError):
49 """Base class for melissa client errors."""
52class ServerError(MelissaError):
53 """Base class for melissa server errors."""
56class ReceptionError(ServerError):
57 """Errors from the reception loop."""
60class TrainingError(ServerError):
61 """Errors from the training."""
64def insert_exception(e: Exception):
65 """Insert an exception into the exception queue."""
66 if e:
67 __EXCEPTION_QUEUE.put(e)
70def check_and_raise_exceptions_from_other_thread() -> None:
71 """Check if there are any exceptions in the queue and raise them."""
72 try:
73 e: Exception = __EXCEPTION_QUEUE.get_nowait()
74 except queue.Empty:
75 return None
77 if "receive" in current_thread().name:
78 raise TrainingError() from e
79 else:
80 raise ReceptionError() from e