Closed Thoufak closed 4 years ago
There's not really any way to do this in the current version, you have to catch it yourself inside the exposed function.
In future it would be good to come up with a better solution for Exceptions. It's not an easy problem to solve, due to the threading you would have to pass the exception across threads and re-raise it. It's doubly complicated because the logical call stack actually starts in JS.
One laborous way (which i usually do) would be what @ChrisKnott suggests. Just mimic how exceptions are handled in an API based model. That is make sure after every exposed function call (API call), there is always a return value in json format. (whether any exception occured or not)
# Not exposed to eel
def actual_work_here(arg):
# Do the actual computation here
return data
@eel.expose
def api(arg):
retval = {'result': True, errmsg: 'Success', 'errcode': 200, 'data': {}}
try:
# TODO: Validate Inputs before calling the actual_work_here function.
x = actual_work_here(arg)
# Set Return Val
retva['data'] = {'someParam': x}
except Exception as e:
retval['result'] = False
retval['errcode'] = 500 # Set this according to http standards or use your own custom defined codes
retval['errmsg'] = str(e)
finally:
return retval
def start_app(arg1):
# Start the server
try:
BASEDIR = os.path.dirname(os.path.abspath(__file__))
web_dir = BASEDIR
start_html_page = 'index.html'
eel.init(web_dir)
logging.info("App Started")
eel.start(start_html_page, port=0)
except Exception as e:
# TODO: Bring out a tkinter popup saying, server could not be laucnhed
err_msg = 'Could not launch a local server'
logging.error('{}\n{}'.format(err_msg, e.args))
logging.info('Closing App')
Obviously you can generalise a lot here. Write a handle exception function and decorate all your exposed functions with that.
P.S. @ChrisKnott I don't think decorators work in eel exposed functions. I think it would be possible to make eel execute the wrapper functions without running into any threading issues. That would make it way easier to handle exceptions in one centralised place.
Thank you very much! I'll look into the provided example.
[Running] python -u "e:\New folder\jarvis\main.py" listening.... recognizing user said:play JavaScript tutorial in YouTube play javascript tutorial in youtube Traceback (most recent call last): File "C:\Users\MANI\AppData\Roaming\Python\Python312\site-packages\eel__init__.py", line 318, in _process_message return_val = _exposed_functionsmessage['name'] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "e:\New folder\jarvis\engine\command.py", line 55, in allCommands PlayYoutube(query) File "e:\New folder\jarvis\engine\features.py", line 32, in PlayYoutube speak("Playing "+search_term+" on YouTube")
TypeError: can only concatenate str (not "NoneType") to str
Describe the problem I want to be able to handle erros raised inside my python functions which are exposed with
eel.expose
. However, it seems that all the exceptions are handled somewhere inside the library.Code snippet(s)
Console output:
Desktop: