client-barcode: Add some network error handling

This commit is contained in:
Sebastian Pipping 2011-10-05 16:11:45 +02:00
parent f3c86688a7
commit c70ec751b1
1 changed files with 29 additions and 13 deletions

View File

@ -12,6 +12,7 @@ import sys
from decimal import Decimal
import os
import time
import urllib2
def clear():
@ -100,20 +101,29 @@ 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:
for (command, balance_backup) in list(self.transfers):
try:
command.run(self.login_name)
self.transfers = None
except urllib2.HTTPError as e:
error_page('FEHLER bei Kommunikation mit Server "%s"' % str(e))
break
else:
self.transfers.pop(0)
if not self.transfers:
# Show final balance for some time
clear()
self.dump()
@ -171,12 +181,18 @@ def handle(line, status):
item_id = int(line) # TODO
try:
item = status.find(item_id)
except: # TODO
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:
try:
status.login(line)
except urllib2.URLError as e:
error_page('FEHLER bei Kommunikation mit Server "%s"' % e.reason)
def main():