summaryrefslogtreecommitdiff
path: root/example.py
diff options
context:
space:
mode:
authorDavid Pursehouse <david.pursehouse@sonymobile.com>2012-10-26 10:45:22 +0900
committerDavid Pursehouse <david.pursehouse@sonymobile.com>2013-01-18 13:23:20 +0900
commitb6380db1cdce80ecfce7f0806b700ddf39ca384a (patch)
tree9130e3f40a7f36cadde197ee6ac4120e33f27435 /example.py
parentbfa3e2b78c64b7c4c9aa15531193d49fe81eccec (diff)
downloadpygerrit-b6380db1cdce80ecfce7f0806b700ddf39ca384a.tar.gz
Better error handling in the example script
Exit with non-zero status if any error occurs when setting up the Gerrit connection or when starting the event stream. Also make sure the event stream is stopped before exiting. Change-Id: Ibf3742b834e30a019fa952440277f9027b8e85ea
Diffstat (limited to 'example.py')
-rwxr-xr-xexample.py20
1 files changed, 17 insertions, 3 deletions
diff --git a/example.py b/example.py
index 601a5f6..6d0bca4 100755
--- a/example.py
+++ b/example.py
@@ -29,9 +29,11 @@ THE SOFTWARE.
import logging
import optparse
import sys
+from threading import Event
import time
from pygerrit.client import GerritClient
+from pygerrit.error import GerritError
from pygerrit.stream import GerritStreamErrorEvent
@@ -55,9 +57,16 @@ def _main():
logging.basicConfig(format='%(message)s', level=logging.INFO)
- gerrit = GerritClient(host=options.hostname)
- logging.info("Connected to Gerrit version [%s]", gerrit.gerrit_version())
- gerrit.start_event_stream()
+ try:
+ gerrit = GerritClient(host=options.hostname)
+ logging.info("Connected to Gerrit version [%s]",
+ gerrit.gerrit_version())
+ gerrit.start_event_stream()
+ except GerritError as err:
+ logging.error("Gerrit error: %s", err)
+ return 1
+
+ errors = Event()
try:
while True:
event = gerrit.get_event(block=options.blocking,
@@ -66,6 +75,7 @@ def _main():
logging.info("Event: %s", str(event))
if isinstance(event, GerritStreamErrorEvent):
logging.error(event.error)
+ errors.set()
break
else:
logging.info("No event")
@@ -73,8 +83,12 @@ def _main():
time.sleep(1)
except KeyboardInterrupt:
logging.info("Terminated by user")
+ finally:
gerrit.stop_event_stream()
+ if errors.isSet():
+ logging.error("Exited with error")
+ return 1
if __name__ == "__main__":
sys.exit(_main())