Skip to content

Commit 9d1b056

Browse files
authored
Merge pull request h2non#70 from kaapstorm/exceptions
Exceptions
2 parents de4df60 + dca10e5 commit 9d1b056

File tree

4 files changed

+52
-10
lines changed

4 files changed

+52
-10
lines changed

jsonpath_ng/exceptions.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class JSONPathError(Exception):
2+
pass
3+
4+
5+
class JsonPathLexerError(JSONPathError):
6+
pass
7+
8+
9+
class JsonPathParserError(JSONPathError):
10+
pass

jsonpath_ng/lexer.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44

55
import ply.lex
66

7-
logger = logging.getLogger(__name__)
8-
7+
from jsonpath_ng.exceptions import JsonPathLexerError
98

10-
class JsonPathLexerError(Exception):
11-
pass
9+
logger = logging.getLogger(__name__)
1210

1311

1412
class JsonPathLexer(object):

jsonpath_ng/parser.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
1-
from __future__ import print_function, absolute_import, division, generators, nested_scopes
1+
from __future__ import (
2+
print_function,
3+
absolute_import,
4+
division,
5+
generators,
6+
nested_scopes,
7+
)
28
import sys
39
import os.path
4-
import logging
510

611
import ply.yacc
712

13+
from jsonpath_ng.exceptions import JsonPathParserError
814
from jsonpath_ng.jsonpath import *
915
from jsonpath_ng.lexer import JsonPathLexer
1016

1117
logger = logging.getLogger(__name__)
1218

19+
1320
def parse(string):
1421
return JsonPathParser().parse(string)
1522

23+
1624
class JsonPathParser(object):
1725
'''
1826
An LALR-parser for JsonPath
@@ -21,8 +29,12 @@ class JsonPathParser(object):
2129
tokens = JsonPathLexer.tokens
2230

2331
def __init__(self, debug=False, lexer_class=None):
24-
if self.__doc__ == None:
25-
raise Exception('Docstrings have been removed! By design of PLY, jsonpath-rw requires docstrings. You must not use PYTHONOPTIMIZE=2 or python -OO.')
32+
if self.__doc__ is None:
33+
raise JsonPathParserError(
34+
'Docstrings have been removed! By design of PLY, '
35+
'jsonpath-rw requires docstrings. You must not use '
36+
'PYTHONOPTIMIZE=2 or python -OO.'
37+
)
2638

2739
self.debug = debug
2840
self.lexer_class = lexer_class or JsonPathLexer # Crufty but works around statefulness in PLY
@@ -66,7 +78,8 @@ def parse_token_stream(self, token_iterator, start_symbol='jsonpath'):
6678
]
6779

6880
def p_error(self, t):
69-
raise Exception('Parse error at %s:%s near token %s (%s)' % (t.lineno, t.col, t.value, t.type))
81+
raise JsonPathParserError('Parse error at %s:%s near token %s (%s)'
82+
% (t.lineno, t.col, t.value, t.type))
7083

7184
def p_jsonpath_binop(self, p):
7285
"""jsonpath : jsonpath '.' jsonpath
@@ -98,7 +111,8 @@ def p_jsonpath_named_operator(self, p):
98111
elif p[1] == 'parent':
99112
p[0] = Parent()
100113
else:
101-
raise Exception('Unknown named operator `%s` at %s:%s' % (p[1], p.lineno(1), p.lexpos(1)))
114+
raise JsonPathParserError('Unknown named operator `%s` at %s:%s'
115+
% (p[1], p.lineno(1), p.lexpos(1)))
102116

103117
def p_jsonpath_root(self, p):
104118
"jsonpath : '$'"

tests/test_exceptions.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import pytest
2+
3+
from jsonpath_ng import parse as rw_parse
4+
from jsonpath_ng.exceptions import JSONPathError, JsonPathParserError
5+
from jsonpath_ng.ext import parse as ext_parse
6+
7+
8+
def test_rw_exception_class():
9+
with pytest.raises(JSONPathError):
10+
rw_parse('foo.bar.`grandparent`.baz')
11+
12+
13+
def test_rw_exception_subclass():
14+
with pytest.raises(JsonPathParserError):
15+
rw_parse('foo.bar.`grandparent`.baz')
16+
17+
18+
def test_ext_exception_subclass():
19+
with pytest.raises(JsonPathParserError):
20+
ext_parse('foo.bar.`grandparent`.baz')

0 commit comments

Comments
 (0)