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,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():