Skip to content

Commit c15a6b6

Browse files
authored
Merge pull request #20 from bentsku/master
Add exception handler for .jsonget for non-existent keys + test
2 parents ba4adf5 + 87da6ba commit c15a6b6

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

rejson/client.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
from redis._compat import (long, nativestr)
66
from .path import Path
77

8+
89
def str_path(p):
910
"Returns the string representation of a path if it is of class Path"
1011
if isinstance(p, Path):
1112
return p.strPath
1213
else:
1314
return p
1415

16+
1517
def bulk_of_jsons(d):
1618
"Replace serialized JSON values with objects in a bulk array response (list)"
1719
def _f(b):
@@ -21,6 +23,7 @@ def _f(b):
2123
return b
2224
return _f
2325

26+
2427
class Client(StrictRedis):
2528
"""
2629
This class subclasses redis-py's `StrictRedis` and implements ReJSON's
@@ -111,7 +114,13 @@ def jsonget(self, name, *args):
111114
else:
112115
for p in args:
113116
pieces.append(str_path(p))
114-
return self.execute_command('JSON.GET', *pieces)
117+
118+
# Handle case where key doesn't exist. The JSONDecoder would raise a
119+
# TypeError exception since it can't decode None
120+
try:
121+
return self.execute_command('JSON.GET', *pieces)
122+
except TypeError:
123+
return None
115124

116125
def jsonmget(self, path, *args):
117126
"""

tests/test_rejson.py

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def testJSONSetGetDelShouldSucceed(self):
2121

2222
self.assertTrue(rj.jsonset('foo', Path.rootPath(), 'bar'))
2323
self.assertEqual('bar', rj.jsonget('foo'))
24+
self.assertEqual(None, rj.jsonget('baz'))
2425
self.assertEqual(1, rj.jsondel('foo'))
2526
self.assertFalse(rj.exists('foo'))
2627

0 commit comments

Comments
 (0)