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
« 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
6from typing import TYPE_CHECKING, Any
8import attrs
10from referencing._attrs import frozen
12if TYPE_CHECKING:
13 from referencing import Resource
14 from referencing.typing import URI
17@frozen
18class NoSuchResource(KeyError):
19 """
20 The given URI is not present in a registry.
22 Unlike most exceptions, this class *is* intended to be publicly
23 instantiable and *is* part of the public API of the package.
24 """
26 ref: URI
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)
33 def __hash__(self) -> int:
34 return hash(attrs.astuple(self))
37@frozen
38class NoInternalID(Exception):
39 """
40 A resource has no internal ID, but one is needed.
42 E.g. in modern JSON Schema drafts, this is the :kw:`$id` keyword.
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 """
48 resource: Resource[Any]
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)
55 def __hash__(self) -> int:
56 return hash(attrs.astuple(self))
59@frozen
60class Unretrievable(KeyError):
61 """
62 The given URI is not present in a registry, and retrieving it failed.
63 """
65 ref: URI
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)
72 def __hash__(self) -> int:
73 return hash(attrs.astuple(self))
76@frozen
77class CannotDetermineSpecification(Exception):
78 """
79 Attempting to detect the appropriate `Specification` failed.
81 This happens if no discernible information is found in the contents of the
82 new resource which would help identify it.
83 """
85 contents: Any
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)
92 def __hash__(self) -> int:
93 return hash(attrs.astuple(self))
96@attrs.frozen # Because here we allow subclassing below.
97class Unresolvable(Exception):
98 """
99 A reference was unresolvable.
100 """
102 ref: URI
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)
109 def __hash__(self) -> int:
110 return hash(attrs.astuple(self))
113@frozen
114class PointerToNowhere(Unresolvable):
115 """
116 A JSON Pointer leads to a part of a document that does not exist.
117 """
119 resource: Resource[Any]
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
132@frozen
133class NoSuchAnchor(Unresolvable):
134 """
135 An anchor does not exist within a particular resource.
136 """
138 resource: Resource[Any]
139 anchor: str
141 def __str__(self) -> str:
142 return (
143 f"{self.anchor!r} does not exist within {self.resource.contents!r}"
144 )
147@frozen
148class InvalidAnchor(Unresolvable):
149 """
150 An anchor which could never exist in a resource was dereferenced.
152 It is somehow syntactically invalid.
153 """
155 resource: Resource[Any]
156 anchor: str
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 )