From c70ec751b1d338237318b90ecea45c127e8fe2af Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Wed, 5 Oct 2011 16:11:45 +0200 Subject: [PATCH] client-barcode: Add some network error handling --- client-barcode/freitagskasse.py | 42 +++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/client-barcode/freitagskasse.py b/client-barcode/freitagskasse.py index 7552c48..d0995bc 100644 --- a/client-barcode/freitagskasse.py +++ b/client-barcode/freitagskasse.py @@ -12,6 +12,7 @@ import sys from decimal import Decimal import os import time +import urllib2 def clear(): @@ -100,27 +101,36 @@ class Status: def login(self, auth_blob): assert(not self.logged_in()) + user_name = net.get_user_name_from_auth_blob(auth_blob) + balance = net.get_balance(user_name) + self.auth_blob = auth_blob self.login_name = user_name - self.balance = net.get_balance(user_name) + self.balance = balance self.transfers = list() def commit(self): assert(self.logged_in()) # Process command queue - for (command, balance_backup) in self.transfers: - command.run(self.login_name) - self.transfers = None + for (command, balance_backup) in list(self.transfers): + try: + command.run(self.login_name) + except urllib2.HTTPError as e: + error_page('FEHLER bei Kommunikation mit Server "%s"' % str(e)) + break + else: + self.transfers.pop(0) - # Show final balance for some time - clear() - self.dump() - delay('Logout', 3) + if not self.transfers: + # Show final balance for some time + clear() + self.dump() + delay('Logout', 3) - # Logout - self._reset() + # Logout + self._reset() def find(self, item_id): try: @@ -171,12 +181,18 @@ def handle(line, status): item_id = int(line) # TODO try: item = status.find(item_id) - except: # TODO - error_page('FEHLER: Aktion oder Ware "%s" nicht bekannt' % line) + except urllib2.HTTPError as e: + if e.code == 404: # URL not found == item not found with REST + error_page('FEHLER: Aktion oder Ware "%s" nicht bekannt' % line) + else: + error_page('FEHLER bei Kommunikation mit Server "%s"' % str(e)) else: status.buy(item) else: - status.login(line) + try: + status.login(line) + except urllib2.URLError as e: + error_page('FEHLER bei Kommunikation mit Server "%s"' % e.reason) def main():