diff options
-rw-r--r-- | gps/client.py | 10 | ||||
-rwxr-xr-x | gpsfake | 3 |
2 files changed, 12 insertions, 1 deletions
diff --git a/gps/client.py b/gps/client.py index b30ad858..541c5ae1 100644 --- a/gps/client.py +++ b/gps/client.py @@ -10,6 +10,11 @@ else: GPSD_PORT="2947" +class json_error: + def __init__(self, data, explanation): + self.data = data + self.explanation = explanation + class gpscommon: "Isolate socket handling and buffering from the protcol interpretation." def __init__(self, host="127.0.0.1", port=GPSD_PORT, verbose=0): @@ -142,7 +147,10 @@ class gpsjson(gpscommon): va = v t[ka] = va return t - self.data = dictwrapper(**asciify(json.loads(buf.strip(), encoding="ascii"))) + try: + self.data = dictwrapper(**asciify(json.loads(buf.strip(), encoding="ascii"))) + except ValueError, e: + raise json_error(buf, e.args[0]) # Should be done for any other array-valued subobjects, too. if self.data["class"] == "SKY" and hasattr(self.data, "satellites"): self.data.satellites = map(lambda x: dictwrapper(**x), self.data.satellites) @@ -195,6 +195,9 @@ if __name__ == '__main__': except socket.error, msg: sys.stderr.write("gpsfake: socket error %s.\n" % msg) raise SystemExit, 1 + except gps.client.json_error, e: + sys.stderr.write("gpsfake: JSON error on line %s is %s.\n" % (`e.data`, e.explanation)) + raise SystemExit, 1 finally: test.cleanup(); |