Coverage for mymeco/exceptions.py: 100%
17 statements
« prev ^ index » next coverage.py v7.4.1, created at 2024-12-21 22:06 +0000
« 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
6MymecoErrorType = typing.TypeVar('MymecoErrorType', bound='MymecoError')
9class MymecoError(BaseException):
10 """
11 All exception raised by Mymeco should inherit this base class.
13 >>> MymecoError('Generic Error')
14 MymecoError: Generic Error
16 >>> print(MymecoError('Unexpected behaviour'))
17 Unexpected behaviour
18 """
20 def __init__(self: MymecoErrorType, message: str) -> None:
21 """
22 Build a standard Mymeco exception.
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
31 def __repr__(self: MymecoErrorType) -> str:
32 """Give detailed information on error."""
33 return '{}: {}'.format(self.__class__.__name__, self.message)
35 def __str__(self: MymecoErrorType) -> str:
36 """Give human readable description on error."""
37 return self.message
40class NoConfigurationFile(MymecoError):
41 """
42 Raise when no configuration file is available.
44 >>> NoConfigurationFile()
45 NoConfigurationFile: No configuration file was found on your system.
46 """
48 def __init__(self) -> None:
49 """Build a missing configuration file exception."""
50 super().__init__('No configuration file was found on your system.')
53class MissingConfiguration(MymecoError):
54 """
55 Raise when a necessary configuration entry is not found.
57 >>> MissingConfiguration('section', 'key', 'file')
58 MissingConfiguration: [section].key is not found in file
59 """
61 def __init__(self,
62 section: str,
63 key: typing.Optional[str],
64 filename: str) -> None:
65 """
66 Build a MissingConfiguration exception.
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))