justpy icon indicating copy to clipboard operation
justpy copied to clipboard

please catch exceptions in callbacks automatically

Open eudoxos opened this issue 4 years ago • 5 comments

It sometimes happen that a callback raises an exception — which is not reported anywhere. I am often writing callbacks like this:

@def callback(*args):
    try:
        do(something)
    except BaseException as e:
        traceback.print_exc()

Perhaps each callback could be automatically wrapped by JustPy so that the exception shows up in the log — I use the following so that it is formatted nicely:

log.error('Exception in '+__name__)
for l in traceback.format_exception(*sys.exc_info()):
    for l2 in l[:-1].split('\n'): log.warning(l2)

Is this the relevant code in JustPy which would have to be wrapped?

https://github.com/elimintz/justpy/blob/f878a28babfa67d5ceccc13afa03e21f97a63708/justpy/htmlcomponents.py#L227-L237

eudoxos avatar Dec 24 '20 08:12 eudoxos

I usually set CRASH to True in the justpy.env file:

# If set to True, the program terminates if there is an error in an event handler
CRASH = config('CRASH', cast=bool, default=False)

Your suggestion is much better for a production environment. It seems to me that a decorator for request handlers that adds the functionality you need is the best option. Alternatively, a configuration variable could be added and then the place to make the change would be the code you posted.

elimintz avatar Dec 24 '20 15:12 elimintz

https://github.com/WolfgangFahl/pyJustpyWidgets/blob/da61fd153c7854b6bbee6a0d12841a87e01da013/jpwidgets/bt5widgets.py#L131 has a handleException function in the App base class.

WolfgangFahl avatar Aug 20 '22 12:08 WolfgangFahl

Hi Wolfgang, I see you assigned a number of issues to me. I won't be available for justpy in foreseeable future unfortunately. Stay well.

On August 20, 2022 2:13:04 PM GMT+02:00, Wolfgang Fahl @.***> wrote:

https://github.com/WolfgangFahl/pyJustpyWidgets/blob/da61fd153c7854b6bbee6a0d12841a87e01da013/jpwidgets/bt5widgets.py#L131 has a handleException function in the App base class.

-- Reply to this email directly or view it on GitHub: https://github.com/elimintz/justpy/issues/196#issuecomment-1221303825 You are receiving this because you were assigned.

Message ID: @.***> -- Sent from my Android device with K-9 Mail. Please excuse my brevity.

eudoxos avatar Aug 27 '22 16:08 eudoxos

@eudoxos thx for getting in touch. Could you please supply a single line pull request (e.g. with a comment line) which i can except so that you show up in the list of collaborators. I simply want to honor your contribution to justpy.

WolfgangFahl avatar Aug 27 '22 19:08 WolfgangFahl

That is kind, thank you; see #465

eudoxos avatar Aug 29 '22 07:08 eudoxos

see #685

WolfgangFahl avatar Sep 20 '23 09:09 WolfgangFahl