proper error handling?

Posted on
  • I have a puck running a simple script that is set to advertise every 2000 ms. There is a timeout set that checks battery every 10 minutes, attaches the value to the name of the device so I can see the status of puck's battery by using a BLE scanner on my phone.
    I am not sure why but sometimes (after days) the advertising string doesn't change anymore (or keeps being empty), puck is still advertising, but the script is obviously not working anymore.
    I believe this is because of some script error that breaks the 10 minutes routine.
    So I would like to put the code in some "try-catch" loop that restarts the whole thing in case something odd happens. How to?
    Thank you!

  • Probably the best idea is to use the hardware watchdog - that's what they have them built in for: https://www.espruino.com/Reference#l_E_e­nableWatchdog

    So:

    E.enableWatchdog(10, false);
    setInterval(function() {
       // your code here
        E.kickWatchdog();
    }, 2000);
    

    If the function doesn't complete to the point that kickWatchdog is called, the whole device will restart.

    But you might also want to look at https://www.espruino.com/Debugger#debugg­ing-when-not-connected - at least then when it stops working you can maybe connect and get some idea what happened?

  • Thank you Gorden, this sounds perfect. Actually I could just set

    E.enableWatchdog(10);
    

    And it will restart the whole thing whenever anything breaks / hangs longer than 10 seconds, right?
    Does the dog take extra power?

  • yes, you can do that, but that's only checking that the idle loop is working. If something happened (low memory, undefined/etc value) that caused your code to keep failing it wouldn't catch that.

    And no, it doesn't add any noticeable power usage :)

  • Thank you Gordon. I have the following code:

    function Timer() {
      [... some more code ...]
      setTimeout(Timer, 60000);
      E.kickWatchdog();
    }
    
    E.enableWatchdog(10, false);
    setTimeout(Timer, 60000);
    

    This is supposed to run some code once per minute.
    But the dog seems to never let it happen this way ... do I need to set the watchdog's timeout higher than my code timeout? The timeout function is not considered "idle" by the watchdog so I need to add this to the time I want to let the code do its thing?

  • That code should work

    do I need to set the watchdog's timeout higher than my code timeout?

    Yes, at least twice - I'd generally go for ~5 times the interval, just in case.

    One thing to note is that once you added the watchdog you can't change it or its interval until the device physically reboots (reset() won't cut it). It's done in hardware as a failsafe so once it's been added, it can't be undone (until a reboot/power cycle)

  • Thank you again, I think I finally understand it – important note indeed!

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

proper error handling?

Posted by Avatar for DanDyse @DanDyse

Actions