Better exception handling

This commit is contained in:
Lynn Ochs 2022-11-08 08:13:43 +01:00
parent d708d1eb21
commit d17552a644
2 changed files with 9 additions and 9 deletions

View File

@ -34,7 +34,7 @@ class Value:
elif callable(translate): elif callable(translate):
self.translate = translate self.translate = translate
else: else:
self.translate = lambda x: translate[x] self.translate = lambda x: translate.get(x, "None")
if start_value is not None: if start_value is not None:
self.update(start_value) self.update(start_value)

View File

@ -10,7 +10,7 @@ from .util import timestamp
from .door.constants import state_names from .door.constants import state_names
class DoorControlSocket(util.Loggable): class DoorControlSocket(util.Loggable):
class LineBuffer(object): class LineBuffer(util.Loggable):
def __init__(self, f, handler): def __init__(self, f, handler):
self.data = b'' self.data = b''
self.f = f self.f = f
@ -20,14 +20,17 @@ class DoorControlSocket(util.Loggable):
data = self.f.recv(1024) data = self.f.recv(1024)
print(repr(data)) print(repr(data))
if not data: if not data:
print("Eep") self._logger().info("Socket closed")
return False return False
self.data += data self.data += data
d = self.data.split(b'\n') d = self.data.split(b'\n')
d, self.data = d[:-1], d[-1] d, self.data = d[:-1], d[-1]
for i in d: for i in d:
print("Handling", repr(i)) self._logger().debug(f"Handling {repr(i)}")
try:
self.handler(self.f, i) self.handler(self.f, i)
except:
self._logger().exception("Error while handling command")
return True return True
def __init__(self, config, control, nfc): def __init__(self, config, control, nfc):
@ -114,10 +117,7 @@ class DoorControlSocket(util.Loggable):
send("Reloading tokens") send("Reloading tokens")
self._nfc._read_valid_tokens() self._nfc._read_valid_tokens()
elif cmd == 'stat': elif cmd == 'stat':
send("Door status is %s, position is %d. Current action: %s\n" % ( send(f"Door status is {self._control.state.str()}, position is {self._control.position()}. Current action: {self._control.target.str()}\n")
self._control.state.str(),
self._control.position(),
self._control.target.str()))
def start(self): def start(self):