Coverage for configzen/errors.py: 77%

26 statements  

« prev     ^ index     » next       coverage.py v7.5.1, created at 2024-05-15 02:42 +0000

1"""Specialized exceptions raised by configzen.""" 

2 

3from __future__ import annotations 1abcdefgh

4 

5from typing import TYPE_CHECKING 1abcdefgh

6 

7if TYPE_CHECKING: 1abcdefgh

8 from configzen.config import BaseConfig 

9 

10 

11__all__ = ( 1abcdefgh

12 "ConfigError", 

13 "ConfigLoadError", 

14 "ConfigReloadError", 

15 "ConfigSaveError", 

16 "NotAMappingError", 

17 "ConfigProcessorError", 

18 "RouteError", 

19 "LinkedRouteError", 

20) 

21 

22 

23class ConfigError(Exception): 1abcdefgh

24 """Base class for all errors related to configzen.""" 

25 

26 def __init__(self, message: str) -> None: 1abcdefgh

27 super().__init__(message) 

28 

29 

30class ConfigLoadError(ConfigError): 1abcdefgh

31 """Raised when the configuration cannot be loaded.""" 

32 

33 

34class ConfigReloadError(ConfigLoadError): 1abcdefgh

35 """Raised when the configuration cannot be reloaded.""" 

36 

37 

38class ConfigSaveError(ConfigError): 1abcdefgh

39 """Raised when the configuration cannot be saved.""" 

40 

41 

42class NotAMappingError(ConfigLoadError, TypeError): 1abcdefgh

43 """Raised when the configuration being loaded is not a mapping.""" 

44 

45 

46class ConfigProcessorError(ConfigError): 1abcdefgh

47 """Raised when a configuration replacement processor error occurs.""" 

48 

49 

50class BaseRouteError(ConfigError, ValueError): 1abcdefgh

51 """Raised when a configuration item route is invalid.""" 

52 

53 

54class RouteError(BaseRouteError): 1abcdefgh

55 """Raised when a configuration item route is invalid at a specific index.""" 

56 

57 def __init__(self, message: str, route: str, index: int) -> None: 1abcdefgh

58 self.message = message 

59 self.route = route 

60 self.index = index 

61 

62 def __str__(self) -> str: 1abcdefgh

63 """Return a string representation of the route error.""" 

64 return f"{self.message} ({self.route}:{self.index})" 

65 

66 

67class LinkedRouteError(BaseRouteError): 1abcdefgh

68 """Raised when a declared configuration item route is invalid.""" 

69 

70 def __init__( 1abcdefgh

71 self, 

72 message: str, 

73 route: str, 

74 config_class: type[BaseConfig], 

75 ) -> None: 

76 self.message = message 1abcdefgh

77 self.route = route 1abcdefgh

78 self.config_class = config_class 1abcdefgh

79 

80 def __str__(self) -> str: 1abcdefgh

81 """Return a string representation of the route error.""" 

82 return f"{self.message} ({self.config_class.__name__}.{self.route})"