Skip to content

Commit 72a7c4f

Browse files
committed
feat: JSON Pointer excpetions
1 parent b9783e8 commit 72a7c4f

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

‎jsonpath/exceptions.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ def __init__(self, *args: object, token: Token) -> None:
7676
self.token = token
7777

7878

79+
class JSONPointerError(Exception):
80+
"""Base class for all JSON Pointer errors."""
81+
82+
83+
class JSONPointerEncodeError(JSONPointerError):
84+
"""An exception raised when a JSONPathMatch can't be encoded to a JSON Pointer."""
85+
86+
87+
class JSONPointerIndexError(JSONPointerError):
88+
"""An exception raised when an array index is out of range."""
89+
90+
7991
def _truncate_message(value: str, num: int, end: str = "...") -> str:
8092
if len(value) < num:
8193
return value

‎jsonpath/pointer.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
from typing import Union
1313
from urllib.parse import unquote
1414

15+
from .exceptions import JSONPointerEncodeError
16+
from .exceptions import JSONPointerIndexError
17+
1518
if TYPE_CHECKING:
1619
from .match import JSONPathMatch
1720

@@ -98,8 +101,7 @@ def _index(self, s: str) -> Union[str, int]:
98101
try:
99102
index = int(s)
100103
if index < self.min_int_index or index > self.max_int_index:
101-
# TODO: exceptions
102-
raise Exception(":(")
104+
raise JSONPointerIndexError("index out of range")
103105
return index
104106
except ValueError:
105107
return s
@@ -133,9 +135,8 @@ def _getitem(obj: Any, key: Any) -> Any:
133135
def from_match(cls, match: JSONPathMatch, *, strict: bool = True) -> JSONPointer:
134136
"""Return a JSON Pointer for the path from a JSONPathMatch instance."""
135137
if strict and "~" in match.parts:
136-
# TODO: better exception
137138
# TODO: reference env for current key selector token
138-
raise Exception(
139+
raise JSONPointerEncodeError(
139140
"can't encode a JSON Pointer containing key or index selectors"
140141
)
141142

0 commit comments

Comments
 (0)