Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/referencing/exceptions.py: 61%

72 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-12-08 06:51 +0000

1""" 

2Errors, oh no! 

3""" 

4from __future__ import annotations 

5 

6from typing import TYPE_CHECKING, Any 

7 

8import attrs 

9 

10from referencing._attrs import frozen 

11 

12if TYPE_CHECKING: 

13 from referencing import Resource 

14 from referencing.typing import URI 

15 

16 

17@frozen 

18class NoSuchResource(KeyError): 

19 """ 

20 The given URI is not present in a registry. 

21 

22 Unlike most exceptions, this class *is* intended to be publicly 

23 instantiable and *is* part of the public API of the package. 

24 """ 

25 

26 ref: URI 

27 

28 def __eq__(self, other: Any) -> bool: 

29 if self.__class__ is not other.__class__: 

30 return NotImplemented 

31 return attrs.astuple(self) == attrs.astuple(other) 

32 

33 def __hash__(self) -> int: 

34 return hash(attrs.astuple(self)) 

35 

36 

37@frozen 

38class NoInternalID(Exception): 

39 """ 

40 A resource has no internal ID, but one is needed. 

41 

42 E.g. in modern JSON Schema drafts, this is the :kw:`$id` keyword. 

43 

44 One might be needed if a resource was to-be added to a registry but no 

45 other URI is available, and the resource doesn't declare its canonical URI. 

46 """ 

47 

48 resource: Resource[Any] 

49 

50 def __eq__(self, other: Any) -> bool: 

51 if self.__class__ is not other.__class__: 

52 return NotImplemented 

53 return attrs.astuple(self) == attrs.astuple(other) 

54 

55 def __hash__(self) -> int: 

56 return hash(attrs.astuple(self)) 

57 

58 

59@frozen 

60class Unretrievable(KeyError): 

61 """ 

62 The given URI is not present in a registry, and retrieving it failed. 

63 """ 

64 

65 ref: URI 

66 

67 def __eq__(self, other: Any) -> bool: 

68 if self.__class__ is not other.__class__: 

69 return NotImplemented 

70 return attrs.astuple(self) == attrs.astuple(other) 

71 

72 def __hash__(self) -> int: 

73 return hash(attrs.astuple(self)) 

74 

75 

76@frozen 

77class CannotDetermineSpecification(Exception): 

78 """ 

79 Attempting to detect the appropriate `Specification` failed. 

80 

81 This happens if no discernible information is found in the contents of the 

82 new resource which would help identify it. 

83 """ 

84 

85 contents: Any 

86 

87 def __eq__(self, other: Any) -> bool: 

88 if self.__class__ is not other.__class__: 

89 return NotImplemented 

90 return attrs.astuple(self) == attrs.astuple(other) 

91 

92 def __hash__(self) -> int: 

93 return hash(attrs.astuple(self)) 

94 

95 

96@attrs.frozen # Because here we allow subclassing below. 

97class Unresolvable(Exception): 

98 """ 

99 A reference was unresolvable. 

100 """ 

101 

102 ref: URI 

103 

104 def __eq__(self, other: Any) -> bool: 

105 if self.__class__ is not other.__class__: 

106 return NotImplemented 

107 return attrs.astuple(self) == attrs.astuple(other) 

108 

109 def __hash__(self) -> int: 

110 return hash(attrs.astuple(self)) 

111 

112 

113@frozen 

114class PointerToNowhere(Unresolvable): 

115 """ 

116 A JSON Pointer leads to a part of a document that does not exist. 

117 """ 

118 

119 resource: Resource[Any] 

120 

121 def __str__(self) -> str: 

122 msg = f"{self.ref!r} does not exist within {self.resource.contents!r}" 

123 if self.ref == "/": 

124 msg += ( 

125 ". The pointer '/' is a valid JSON Pointer but it points to " 

126 "an empty string property ''. If you intended to point " 

127 "to the entire resource, you should use '#'." 

128 ) 

129 return msg 

130 

131 

132@frozen 

133class NoSuchAnchor(Unresolvable): 

134 """ 

135 An anchor does not exist within a particular resource. 

136 """ 

137 

138 resource: Resource[Any] 

139 anchor: str 

140 

141 def __str__(self) -> str: 

142 return ( 

143 f"{self.anchor!r} does not exist within {self.resource.contents!r}" 

144 ) 

145 

146 

147@frozen 

148class InvalidAnchor(Unresolvable): 

149 """ 

150 An anchor which could never exist in a resource was dereferenced. 

151 

152 It is somehow syntactically invalid. 

153 """ 

154 

155 resource: Resource[Any] 

156 anchor: str 

157 

158 def __str__(self) -> str: 

159 return ( 

160 f"'#{self.anchor}' is not a valid anchor, neither as a " 

161 "plain name anchor nor as a JSON Pointer. You may have intended " 

162 f"to use '#/{self.anchor}', as the slash is required *before each " 

163 "segment* of a JSON pointer." 

164 )