From b6380db1cdce80ecfce7f0806b700ddf39ca384a Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Fri, 26 Oct 2012 10:45:22 +0900 Subject: 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 --- example.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'example.py') 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()) -- cgit v1.2.1