Coverage for src/gentrie/exceptions.py: 100%

29 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-08-20 09:30 -0700

1# -*- coding: utf-8 -*- 

2"""Custom exceptions for the gentrie package.""" 

3from enum import Enum 

4 

5 

6class ErrorTag(str, Enum): 

7 """Tags for error path identification for tests for the gentrie packages. 

8 

9 ErrorTags' are used to identify specific error conditions in the gentrie package. 

10 Tests use these tags to assert specific error condition paths. 

11 """ 

12 # __getitem__() tags 

13 GETITEM_ID_NOT_FOUND = "GETITEM_ID_NOT_FOUND" 

14 GETITEM_KEY_NOT_FOUND = "GETITEM_KEY_NOT_FOUND" 

15 GETITEM_INVALID_KEY_TYPE = "GETITEM_INVALID_KEY_TYPE" 

16 GETITEM_NOT_TERMINAL = "GETITEM_NOT_TERMINAL" 

17 

18 # removal() tags 

19 REMOVAL_INVALID_KEY_TYPE = "REMOVAL_INVALID_KEY_TYPE" 

20 REMOVAL_KEY_NOT_FOUND = "REMOVAL_KEY_NOT_FOUND" 

21 

22 # prefixes() tags 

23 TRIE_PREFIXES_INVALID_GENERALIZED_KEY = "TRIE_PREFIXES_INVALID_GENERALIZED_KEY" 

24 

25 # prefixed_by() tags 

26 TRIE_PREFIXED_BY_INVALID_GENERALIZED_KEY = "TRIE_PREFIXED_BY_INVALID_GENERALIZED_KEY" 

27 TRIE_PREFIXED_BY_BAD_DEPTH_TYPE = "TRIE_PREFIXED_BY_BAD_DEPTH_TYPE" 

28 TRIE_PREFIXED_BY_BAD_DEPTH_VALUE = "TRIE_PREFIXED_BY_BAD_DEPTH_VALUE" 

29 

30 # _store_entry() tags 

31 STORE_ENTRY_INVALID_GENERALIZED_KEY = "STORE_ENTRY_INVALID_GENERALIZED_KEY" 

32 STORE_ENTRY_DUPLICATE_KEY = "STORE_ENTRY_DUPLICATE_KEY" 

33 

34 

35class TrieTypeError(TypeError): 

36 """Base class for all trie-related type errors. 

37 

38 It differs from a standard TypeError by the addition of a 

39 tag code used to very specifically identify where the error 

40 was thrown in the code for testing and development support. 

41 

42 This tag code does not have a direct semantic meaning except to identify 

43 the specific code throwing the exception for tests. 

44 

45 Args: 

46 msg (str): The error message. 

47 tag (ErrorTag): The tag code. 

48 """ 

49 def __init__(self, msg: str, tag: ErrorTag) -> None: 

50 """Create a new TrieTypeError. 

51 

52 Args: 

53 msg (str): The error message. 

54 tag (str): The tag code. 

55 """ 

56 self.tag_code: ErrorTag = tag 

57 super().__init__(msg) 

58 

59 

60class TrieKeyError(KeyError): 

61 """Base class for all trie-related key errors. 

62 

63 It differs from a standard KeyError by the addition of a 

64 tag code used to very specifically identify where the error 

65 was thrown in the code for testing and development support. 

66 

67 This tag code does not have a direct semantic meaning except to identify 

68 the specific code throwing the exception for tests. 

69 

70 Args: 

71 msg (str): The error message. 

72 tag (ErrorTag): The tag code. 

73 """ 

74 def __init__(self, msg: str, tag: ErrorTag) -> None: 

75 self.tag_code: ErrorTag = tag 

76 super().__init__(msg) 

77 

78 

79class TrieValueError(ValueError): 

80 """Base class for all trie-related value errors. 

81 

82 It differs from a standard ValueError by the addition of a 

83 tag code used to very specifically identify where the error 

84 was thrown in the code for testing and development support. 

85 

86 This tag code does not have a direct semantic meaning except to identify 

87 the specific code throwing the exception for tests. 

88 

89 Args: 

90 msg (str): The error message. 

91 tag (ErrorTag): The tag code. 

92 """ 

93 def __init__(self, msg: str, tag: ErrorTag) -> None: 

94 self.tag_code: ErrorTag = tag 

95 super().__init__(msg) 

96 

97 

98class InvalidTrieKeyTokenError(TrieTypeError): 

99 """Raised when a token in a key is not a valid :class:`TrieKeyToken` object. 

100 

101 This is a sub-class of :class:`TrieTypeError`.""" 

102 

103 

104class InvalidGeneralizedKeyError(TrieTypeError): 

105 """Raised when a key is not a valid :class:`GeneralizedKey` object. 

106 

107 This is a sub-class of :class:`TrieTypeError`.""" 

108 

109 

110class DuplicateKeyError(TrieKeyError): 

111 """Raised when an attempt is made to add a key that is already in the trie 

112 with a different associated value. 

113 

114 This is a sub-class of :class:`TrieKeyError`."""