Best method to catch random errors?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Divergentti
Posts: 67
Joined: Fri Sep 04, 2020 9:27 am
Location: Hanko, Finland
Contact:

Best method to catch random errors?

Post by Divergentti » Sat Feb 27, 2021 12:29 pm

With Python try - except is method to catch errors https://docs.python.org/3/tutorial/errors.html. Except should not be too broad, but excepts can be nested to cover most known errors. Programmer shall try to figure out all scenarios where error may raise.

What about random errors, which programmer did not cover in the try - except scenario? Should programmer implement some sort of "de facto catch all exceptions" with main execution? Problem is even more complex to handle with ULP scenarios, where processor sleeps most of the time and then seldomly something goes wrong.

I had in this code https://github.com/divergentti/airquali ... or/main.py error which I was not able to catch via WebREPL. Every now and then the code crashed and processor did not sleep.

Finally, after trying to understand what may fail and tested all kinds of things, I implemented boottimelogger so that I see in which part of the code crashes. From the boottimelog I figured that seldom crahses has to be related to solve_dst_and_set_time() and indeed, after running several times solve_dst_and_set_time() I got a ValueError, which was not covered by the try - except. Something goes wrong with ntptime.settime(), which I have not yet figured. Relates to long integers (error: npttime.settime() error overflow converting long int to machine word)

From bug hunting point of view, try - except is most likely not best practise. Boottime logging seems to work as I implemented it, but best apporach might be logging console to logfile, but how to catch code breaking codes to the logfile? Or is there some better ways to hunt bugs?
Last edited by Divergentti on Sun Feb 28, 2021 8:21 am, edited 1 time in total.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Best method to catch random errors?

Post by dhylands » Sat Feb 27, 2021 3:08 pm

Here’s an example of catching an exception and logging to a file: https://github.com/dhylands/upy-example ... int_exc.py

Divergentti
Posts: 67
Joined: Fri Sep 04, 2020 9:27 am
Location: Hanko, Finland
Contact:

Re: Best method to catch random errors?

Post by Divergentti » Sat Feb 27, 2021 3:51 pm

dhylands wrote:
Sat Feb 27, 2021 3:08 pm
Here’s an example of catching an exception and logging to a file: https://github.com/dhylands/upy-example ... int_exc.py
Thank you. I did not know sys.print_exception. I had:

Code: Select all

if __name__ == "__main__":
    f4.write("Execute main\n")
    try:
        main()
    except Exception as e:
        f4.write("Exception %s" % e)
        if type(e).__name__ == "MQTTException":
            if DEBUG_ENABLED == 1:
                print("** ERROR: Check MQTT server connection info, username and password! **")
            error_reporting("ERROR: Check MQTT server connection info, username and password!")
            f4.close()
            raise
        elif type(e).__name__ == "OSError":
            if DEBUG_ENABLED == 1:
                print("OSError %s! Booting in 10 seconds." % e)
            f4.close()
            error_reporting("OSError %s" % e)
            sleep(10)
            reset()
        elif type(e).__name__ == "MemoryError":
            if DEBUG_ENABLED == 1:
                print("Memory Error %s! Booting in 10 seconds." % e)
            f4.close()
            sleep(10)
            reset()
        elif type(e).__name__ == "KeyboardInterrupt":
            f4.close()
            raise
which looks insane compared to sys.print_exception :lol:

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: Best method to catch random errors?

Post by davef » Mon May 31, 2021 9:23 am

Hi Dave,

I think one of my last problems I need to tackle is catching random errors. I am using the "Simple software WDT implementation" viewtopic.php?t=5517&start=10. I have modified it so that I get the time that the watchdog times-out. This usually helps to find problems when periodically calling another .py file but it doesn't help when my 300-400 line logging program "falls-over".

Any suggestions?
Thanks

Post Reply