Coverage for src/gentrie/exceptions.py: 100%
29 statements
« prev ^ index » next coverage.py v7.6.10, created at 2025-08-17 12:20 -0700
« prev ^ index » next coverage.py v7.6.10, created at 2025-08-17 12:20 -0700
1# -*- coding: utf-8 -*-
2"""Custom exceptions for the gentrie package."""
3from enum import Enum
6class ErrorTag(str, Enum):
7 """Tags for error path identification for tests for the gentrie packages.
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"
18 # removal() tags
19 REMOVAL_INVALID_KEY_TYPE = "REMOVAL_INVALID_KEY_TYPE"
20 REMOVAL_KEY_NOT_FOUND = "REMOVAL_KEY_NOT_FOUND"
22 # prefixes() tags
23 TRIE_PREFIXES_INVALID_GENERALIZED_KEY = "TRIE_PREFIXES_INVALID_GENERALIZED_KEY"
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"
30 # _store_entry() tags
31 STORE_ENTRY_INVALID_GENERALIZED_KEY = "STORE_ENTRY_INVALID_GENERALIZED_KEY"
32 STORE_ENTRY_DUPLICATE_KEY = "STORE_ENTRY_DUPLICATE_KEY"
35class TrieTypeError(TypeError):
36 """Base class for all trie-related type errors.
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.
42 This tag code does not have a direct semantic meaning except to identify
43 the specific code throwing the exception for tests.
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.
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)
60class TrieKeyError(KeyError):
61 """Base class for all trie-related key errors.
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.
67 This tag code does not have a direct semantic meaning except to identify
68 the specific code throwing the exception for tests.
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)
79class TrieValueError(ValueError):
80 """Base class for all trie-related value errors.
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.
86 This tag code does not have a direct semantic meaning except to identify
87 the specific code throwing the exception for tests.
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)
98class InvalidTrieKeyTokenError(TrieTypeError):
99 """Raised when a token in a key is not a valid :class:`TrieKeyToken` object.
101 This is a sub-class of :class:`TrieTypeError`."""
104class InvalidGeneralizedKeyError(TrieTypeError):
105 """Raised when a key is not a valid :class:`GeneralizedKey` object.
107 This is a sub-class of :class:`TrieTypeError`."""
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.
114 This is a sub-class of :class:`TrieKeyError`."""