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

1import queue 

2from threading import current_thread 

3 

4__EXCEPTION_QUEUE: queue.Queue = queue.Queue() 

5 

6 

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) 

13 

14 

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) 

28 

29 

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.""" 

34 

35 

36class ConfigurationFileError(FatalError): 

37 """Errors from the configuration file.""" 

38 

39 

40class InitialConnectionError(FatalError): 

41 """Errors coming while connecting to the launcher.""" 

42 

43 

44class UnsupportedProtocol(InitialConnectionError): 

45 """Protocol Exception.""" 

46 

47 

48class ClientError(MelissaError): 

49 """Base class for melissa client errors.""" 

50 

51 

52class ServerError(MelissaError): 

53 """Base class for melissa server errors.""" 

54 

55 

56class ReceptionError(ServerError): 

57 """Errors from the reception loop.""" 

58 

59 

60class TrainingError(ServerError): 

61 """Errors from the training.""" 

62 

63 

64def insert_exception(e: Exception): 

65 """Insert an exception into the exception queue.""" 

66 if e: 

67 __EXCEPTION_QUEUE.put(e) 

68 

69 

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 

76 

77 if "receive" in current_thread().name: 

78 raise TrainingError() from e 

79 else: 

80 raise ReceptionError() from e