Skip to content

Commit

Permalink
fix: use catch_error in loading_button
Browse files Browse the repository at this point in the history
  • Loading branch information
12rambau committed Sep 17, 2023
1 parent 894c676 commit 2903c2a
Showing 1 changed file with 45 additions and 40 deletions.
85 changes: 45 additions & 40 deletions sepal_ui/scripts/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 2903c2a

Please sign in to comment.