Handle timeout error

This commit is contained in:
Bridget
2020-04-13 16:51:51 +02:00
parent 83ec087f7b
commit 95530fd468
2 changed files with 14 additions and 2 deletions

2
lib.py
View File

@@ -39,6 +39,8 @@ def send_cmd(url: str, secret_key: str, cmd: str) -> Dict:
raise SessionLockedError("Error: " + e.response.json().get("error"))
elif code == 401:
raise UnauthorizedError("Error: " + e.response.json().get("error"))
elif code == 504:
raise TimeoutError("Error: " + e.response.content.decode("UTF-8"))
else:
raise e

View File

@@ -44,6 +44,17 @@ class Shell(cmd.Cmd):
def emptyline(self):
return None
def _send_cmd(self, command: str) -> Dict:
if not self.url:
raise Exception("URL not yet available")
try:
result = send_cmd(self.url, SECRET_KEY, command).get("result")
except TimeoutError as e:
return str(e)
return result
def do_connect(self, url):
"""Connect to given URL."""
if self.url:
@@ -98,8 +109,7 @@ class Shell(cmd.Cmd):
"""Make a new command available within the shell."""
def do_cmd(arg):
result = send_cmd(self.url, SECRET_KEY, f"{do_cmd.__name__} {arg}")
print(result.get("result"))
print(self._send_cmd(f"{do_cmd.__name__} {arg}"))
do_cmd.__doc__ = help_text
do_cmd.__name__ = name