Coverage for mymeco/exceptions.py: 100%

17 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-12-21 22:06 +0000

1# coding: utf-8 

2"""Top Level exceptions module raised by Mymeco.""" 

3import typing 

4 

5 

6MymecoErrorType = typing.TypeVar('MymecoErrorType', bound='MymecoError') 

7 

8 

9class MymecoError(BaseException): 

10 """ 

11 All exception raised by Mymeco should inherit this base class. 

12 

13 >>> MymecoError('Generic Error') 

14 MymecoError: Generic Error 

15 

16 >>> print(MymecoError('Unexpected behaviour')) 

17 Unexpected behaviour 

18 """ 

19 

20 def __init__(self: MymecoErrorType, message: str) -> None: 

21 """ 

22 Build a standard Mymeco exception. 

23 

24 :param message: Human readable information on exception. 

25 Message should help end-user to understand why such error occurs, 

26 and possibly some hints on how to solve issue. 

27 """ 

28 super().__init__() 

29 self.message = message 

30 

31 def __repr__(self: MymecoErrorType) -> str: 

32 """Give detailed information on error.""" 

33 return '{}: {}'.format(self.__class__.__name__, self.message) 

34 

35 def __str__(self: MymecoErrorType) -> str: 

36 """Give human readable description on error.""" 

37 return self.message 

38 

39 

40class NoConfigurationFile(MymecoError): 

41 """ 

42 Raise when no configuration file is available. 

43 

44 >>> NoConfigurationFile() 

45 NoConfigurationFile: No configuration file was found on your system. 

46 """ 

47 

48 def __init__(self) -> None: 

49 """Build a missing configuration file exception.""" 

50 super().__init__('No configuration file was found on your system.') 

51 

52 

53class MissingConfiguration(MymecoError): 

54 """ 

55 Raise when a necessary configuration entry is not found. 

56 

57 >>> MissingConfiguration('section', 'key', 'file') 

58 MissingConfiguration: [section].key is not found in file 

59 """ 

60 

61 def __init__(self, 

62 section: str, 

63 key: typing.Optional[str], 

64 filename: str) -> None: 

65 """ 

66 Build a MissingConfiguration exception. 

67 

68 :param section: Name of section with missing entry 

69 :param key: Name of missing key in section. Could be None if even the 

70 given section is not found. 

71 :param filename: Configuration filename used in this context. 

72 """ 

73 super().__init__('[{}].{} is not found in {}'.format( 

74 section, key, filename))