summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Pursehouse <david.pursehouse@sonymobile.com>2012-09-12 11:11:59 +0900
committerDavid Pursehouse <david.pursehouse@sonymobile.com>2012-09-12 11:42:08 +0900
commitb961935ad0d663340d7ccb620af672ad338ea974 (patch)
tree3252988051195e169d2ca66f47a58968ea0c791f
parent894bc5d6d219509e69c7761afcd4f399036306e7 (diff)
downloadpygerrit-b961935ad0d663340d7ccb620af672ad338ea974.tar.gz
Handle errors when reading event stream
When setting up and reading the gerrit event stream, catch errors and dispatch them to the gerrit client as a new error event type, `GerritStreamErrorEvent`. Change-Id: Ide51dc4882450dca67ee419f3702aa4fb415cf09
-rw-r--r--pygerrit/stream.py41
1 files changed, 29 insertions, 12 deletions
diff --git a/pygerrit/stream.py b/pygerrit/stream.py
index 7026213..c786ad0 100644
--- a/pygerrit/stream.py
+++ b/pygerrit/stream.py
@@ -9,6 +9,18 @@ from select import poll, POLLIN
from threading import Thread, Event
from pygerrit.ssh import GerritSSHClient
+from pygerrit.error import GerritError
+from pygerrit.events import GerritEvent, GerritEventFactory
+
+
+@GerritEventFactory.register("gerrit-stream-error")
+class GerritStreamErrorEvent(GerritEvent):
+
+ """ Represents an error when handling the gerrit event stream """
+
+ def __init__(self, json_data):
+ super(GerritStreamErrorEvent, self).__init__()
+ self.error = json_data["error"]
class GerritStream(Thread):
@@ -28,15 +40,20 @@ class GerritStream(Thread):
def run(self):
""" Listen to the stream and send events to the client. """
- client = GerritSSHClient(self._host)
- _stdin, stdout, _stderr = client.run_gerrit_command("stream-events")
- p = poll()
- p.register(stdout.channel)
- while not self._stop.is_set():
- data = p.poll()
- for (fd, event) in data:
- if fd == stdout.channel.fileno():
- if event == POLLIN:
- line = stdout.readline()
- json_data = json.loads(line)
- self._gerrit.put_event(json_data)
+ try:
+ client = GerritSSHClient(self._host)
+ _stdin, stdout, _stderr = client.run_gerrit_command("stream-events")
+ p = poll()
+ p.register(stdout.channel)
+ while not self._stop.is_set():
+ data = p.poll()
+ for (fd, event) in data:
+ if fd == stdout.channel.fileno():
+ if event == POLLIN:
+ line = stdout.readline()
+ json_data = json.loads(line)
+ self._gerrit.put_event(json_data)
+ except GerritError, e:
+ error = json.loads('{"type":"gerrit-stream-error",'
+ '"error":"%s"}' % str(e))
+ self._gerrit.put_event(error)