From 3a2da0282fa76d592699a890d3f8fab618fc4ea8 Mon Sep 17 00:00:00 2001 From: Huy Pham <42948410+hpham@users.noreply.github.com> Date: Thu, 20 Feb 2020 15:46:18 -0800 Subject: [PATCH 1/2] display error dialog when start session fails --- daemon/core/gui/errors.py | 9 +++++++++ daemon/core/gui/task.py | 15 +++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/daemon/core/gui/errors.py b/daemon/core/gui/errors.py index b6152489..51c90e35 100644 --- a/daemon/core/gui/errors.py +++ b/daemon/core/gui/errors.py @@ -36,3 +36,12 @@ def show_grpc_error(e: "grpc.RpcError", master, app: "Application"): title = f"GRPC {title}" dialog = ErrorDialog(master, app, title, e.details()) dialog.show() + + +def show_grpc_response_exceptions(class_name, exceptions, master, app: "Application"): + title = f"Exceptions from {class_name}" + detail = "" + for e in exceptions: + detail = detail + f"{e}\n" + dialog = ErrorDialog(master, app, title, detail) + dialog.show() diff --git a/daemon/core/gui/task.py b/daemon/core/gui/task.py index bd7423ee..2863326f 100644 --- a/daemon/core/gui/task.py +++ b/daemon/core/gui/task.py @@ -2,6 +2,8 @@ import logging import threading from typing import Any, Callable +from core.gui.errors import show_grpc_response_exceptions + class BackgroundTask: def __init__(self, master: Any, task: Callable, callback: Callable = None, args=()): @@ -19,6 +21,19 @@ class BackgroundTask: def run(self): result = self.task(*self.args) logging.info("task completed") + # if start session fails, a response with Result: False and a list of exceptions is returned + if hasattr(result, "result") and not result.result: + if hasattr(result, "exceptions") and len(result.exceptions) > 0: + self.master.after( + 0, + show_grpc_response_exceptions, + *( + result.__class__.__name__, + result.exceptions, + self.master, + self.master, + ) + ) if self.callback: if result is None: args = () From 7a50f6ac25981ef89f5e2b2345e985b8f9276f6a Mon Sep 17 00:00:00 2001 From: Huy Pham <42948410+hpham@users.noreply.github.com> Date: Mon, 24 Feb 2020 11:24:59 -0800 Subject: [PATCH 2/2] replace hasattr with getattr for cleaner code --- daemon/core/gui/task.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/daemon/core/gui/task.py b/daemon/core/gui/task.py index 2863326f..eb6655f8 100644 --- a/daemon/core/gui/task.py +++ b/daemon/core/gui/task.py @@ -22,8 +22,8 @@ class BackgroundTask: result = self.task(*self.args) logging.info("task completed") # if start session fails, a response with Result: False and a list of exceptions is returned - if hasattr(result, "result") and not result.result: - if hasattr(result, "exceptions") and len(result.exceptions) > 0: + if not getattr(result, "result", True): + if len(getattr(result, "exceptions", [])) > 0: self.master.after( 0, show_grpc_response_exceptions,