Coverage for sources / detextive / exceptions.py: 100%

59 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-02-17 06:15 +0000

1# vim: set filetype=python fileencoding=utf-8: 

2# -*- coding: utf-8 -*- 

3 

4#============================================================================# 

5# # 

6# Licensed under the Apache License, Version 2.0 (the "License"); # 

7# you may not use this file except in compliance with the License. # 

8# You may obtain a copy of the License at # 

9# # 

10# http://www.apache.org/licenses/LICENSE-2.0 # 

11# # 

12# Unless required by applicable law or agreed to in writing, software # 

13# distributed under the License is distributed on an "AS IS" BASIS, # 

14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # 

15# See the License for the specific language governing permissions and # 

16# limitations under the License. # 

17# # 

18#============================================================================# 

19 

20 

21''' Family of exceptions for package API. ''' 

22 

23 

24from . import __ 

25from . import nomina as _nomina 

26 

27 

28class Omniexception( __.immut.exceptions.Omniexception ): 

29 ''' Base for all exceptions raised by package API. ''' 

30 

31 

32class Omnierror( Omniexception, Exception ): 

33 ''' Base for error exceptions raised by package API. ''' 

34 

35 

36class BehaviorsInvalidity( Omnierror, TypeError, ValueError ): 

37 

38 def __init__( self, attribute: str, expectation: str ) -> None: 

39 message = ( 

40 f"Behaviors attribute '{attribute}' must be {expectation}" ) 

41 super( ).__init__( f"{message}." ) 

42 

43 

44class CharsetDetectFailure( Omnierror, TypeError, ValueError ): 

45 

46 def __init__( 

47 self, location: __.Absential[ _nomina.Location ] = __.absent 

48 ) -> None: 

49 message = "Could not detect character set for content" 

50 if not __.is_absent( location ): 

51 message = f"{message} at '{location}'" 

52 super( ).__init__( f"{message}." ) 

53 

54 

55class CharsetInferFailure( Omnierror, TypeError, ValueError ): 

56 

57 def __init__( 

58 self, location: __.Absential[ _nomina.Location ] = __.absent 

59 ) -> None: 

60 message = "Could not infer character set for content" 

61 if not __.is_absent( location ): 

62 message = f"{message} at '{location}'" 

63 super( ).__init__( f"{message}." ) 

64 

65 

66class ContentDecodeImpossibility( Omnierror, TypeError, ValueError ): 

67 

68 def __init__( 

69 self, location: __.Absential[ _nomina.Location ] = __.absent 

70 ) -> None: 

71 message = "Could not decode probable non-textual content" 

72 if not __.is_absent( location ): 

73 message = f"{message} at '{location}'" 

74 super( ).__init__( f"{message}." ) 

75 

76 

77class ContentDecodeFailure( Omnierror, UnicodeError ): 

78 

79 def __init__( 

80 self, 

81 charset: str | __.cabc.Sequence[ str ], 

82 location: __.Absential[ _nomina.Location ] = __.absent, 

83 ) -> None: 

84 message = "Could not decode content" 

85 if not __.is_absent( location ): 

86 message = f"{message} at '{location}'" 

87 if isinstance( charset, str ): charset = ( charset, ) 

88 charsets = ', '.join( f"'{charset_}'" for charset_ in charset ) 

89 message = f"{message} with character sets {charsets}" 

90 super( ).__init__( f"{message}." ) 

91 

92 

93class MimetypeDetectFailure( Omnierror, TypeError, ValueError ): 

94 

95 def __init__( 

96 self, location: __.Absential[ _nomina.Location ] = __.absent 

97 ) -> None: 

98 # TODO: Add 'reason' argument. 

99 message = "Could not detect MIME type for content" 

100 if not __.is_absent( location ): 

101 message = f"{message} at '{location}'" 

102 super( ).__init__( f"{message}." ) 

103 

104 

105class MimetypeInferFailure( Omnierror, TypeError, ValueError ): 

106 

107 def __init__( 

108 self, location: __.Absential[ _nomina.Location ] = __.absent 

109 ) -> None: 

110 message = "Could not infer MIME type for content" 

111 if not __.is_absent( location ): 

112 message = f"{message} at '{location}'" 

113 super( ).__init__( f"{message}." ) 

114 

115 

116class TextInvalidity( Omnierror, TypeError, ValueError ): 

117 

118 def __init__( 

119 self, location: __.Absential[ _nomina.Location ] = __.absent 

120 ) -> None: 

121 # TODO: Add 'reason' argument. 

122 message = "Text is not valid" 

123 if not __.is_absent( location ): 

124 message = f"{message} at '{location}'" 

125 super( ).__init__( f"{message}." ) 

126 

127 

128class TextualMimetypeInvalidity( Omnierror, ValueError ): 

129 

130 def __init__( 

131 self, 

132 mimetype: str, 

133 location: __.Absential[ _nomina.Location ] = __.absent, 

134 ) -> None: 

135 message = f"MIME type '{mimetype}' is not textual for content" 

136 if not __.is_absent( location ): 

137 message = f"{message} at '{location}'" 

138 super( ).__init__( f"{message}." )