From 2903c2af4223f08853748e2f336e92f5a797ed85 Mon Sep 17 00:00:00 2001 From: Pierrick Rambaud Date: Sun, 17 Sep 2023 21:01:54 +0200 Subject: [PATCH] fix: use catch_error in loading_button --- sepal_ui/scripts/decorator.py | 85 ++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 40 deletions(-) diff --git a/sepal_ui/scripts/decorator.py b/sepal_ui/scripts/decorator.py index eb0e228d..81087c38 100644 --- a/sepal_ui/scripts/decorator.py +++ b/sepal_ui/scripts/decorator.py @@ -88,7 +88,40 @@ def wrapper_alert_error(self, *args, **kwargs): # try to execute the method value = None try: - value = func(self, *args, **kwargs) + # Catch warnings in the process function + with warnings.catch_warnings(record=True) as w_list: + value = func(self, *args, **kwargs) + + # Check if there are warnings in the function and append them + # Use append msg as several warnings could be triggered + if w_list: + + # split the warning list + w_list_sepal = [ + w for w in w_list if isinstance(w.message, SepalWarning) + ] + + # display the sepal one + ms_list = [ + f"{w.category.__name__}: {w.message.args[0]}" + for w in w_list_sepal + ] + [alert_.append_msg(ms, type_="warning") for ms in ms_list] + + # only display them in the console if debug mode + if debug: + + def custom_showwarning(w): + return warnings.showwarning( + message=w.message, + category=w.category, + filename=w.filename, + lineno=w.lineno, + line=w.line, + ) + + [custom_showwarning(w) for w in w_list] + except Exception as e: alert_.add_msg(f"{e}", type_="error") if debug: @@ -162,49 +195,21 @@ def wrapper_loading(self, *args, **kwargs): alert_.reset() button_.toggle_loading() # Start loading - value = None - try: - # Catch warnings in the process function - with warnings.catch_warnings(record=True) as w_list: - value = func(self, *args, **kwargs) - - # Check if there are warnings in the function and append them - # Use append msg as several warnings could be triggered - if w_list: - - # split the warning list - w_list_sepal = [ - w for w in w_list if isinstance(w.message, SepalWarning) - ] - - # display the sepal one - ms_list = [ - f"{w.category.__name__}: {w.message.args[0]}" - for w in w_list_sepal - ] - [alert_.append_msg(ms, type_="warning") for ms in ms_list] - - # only display them in the console if debug mode - if debug: - - def custom_showwarning(w): - return warnings.showwarning( - message=w.message, - category=w.category, - filename=w.filename, - lineno=w.lineno, - line=w.line, - ) - [custom_showwarning(w) for w in w_list] + # run the function using the catch_error decorator + decorated, value = catch_errors(alert=alert_, debug=debug)(func), None + try: + value = decorated(self, *args, **kwargs) - except Exception as e: - alert_.add_msg(f"{e}", type_="error") + # exception can only be raised if debug is set to True + # nevertheless and as a reminder, we check if debug is set to True + # and stop the loading state if it's the case + except Exception: if debug: - button_.toggle_loading() # Stop loading button if there is an error - raise e + button_.toggle_loading() - button_.toggle_loading() # Stop loading button + # normal behavior where we stop the loading state after the function is executed + button_.toggle_loading() return value