Coverage for /Users/ajo/work/jumpstarter/jumpstarter/packages/jumpstarter/jumpstarter/common/exceptions.py: 65%

23 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-05-06 10:20 +0200

1import sys 

2 

3 

4class JumpstarterException(Exception): 

5 """Base class for jumpstarter-specific errors. 

6 

7 This class should not be raised directly, but should be used as a base 

8 class for all jumpstarter-specific errors. 

9 It handles the __cause__ attribute so the jumpstarter errors could be raised as 

10 

11 raise SomeError("message") from original_exception 

12 """ 

13 

14 def __init__(self, message: str): 

15 super().__init__(message) 

16 self.message = message 

17 

18 def __str__(self): 

19 if self.__cause__: 

20 return f"{self.message} (Caused by: {self.__cause__})" 

21 return f"{self.message}" 

22 

23 def print(self, message: str | None = None): 

24 ANSI_RED = "\033[91m" 

25 ANSI_CLEAR = "\033[0m" 

26 print(f"{ANSI_RED}{self}{ANSI_CLEAR}", file=sys.stderr) 

27 

28 

29class ConnectionError(JumpstarterException): 

30 """Raised when a connection to a jumpstarter server fails.""" 

31 

32 pass 

33 

34 

35class ConfigurationError(JumpstarterException): 

36 """Raised when a configuration error exists.""" 

37 

38 pass 

39 

40 

41class ArgumentError(JumpstarterException): 

42 """Raised when a cli argument is not valid.""" 

43 

44 pass 

45 

46 

47class FileAccessError(JumpstarterException): 

48 """Raised when a file access error occurs.""" 

49 

50 pass 

51 

52 

53class FileNotFoundError(JumpstarterException, FileNotFoundError): 

54 """Raised when a file is not found.""" 

55 

56 pass