Do you see why this code generate an unhandled exception?

It's been reported through ERA but I fail to see why it would. I haven't seen it myself. The Unhandled Exception is at the Bg.exit line and i've seen both conditions without any error.

	(:background_method)
    function onReceiveToken(responseCode, data) {
		var result;
        if (responseCode == 200) {
        	result = { "Token" => data };
        } else {
			result = { "httpErrorTesla" => responseCode };
	    }
		Bg.exit({ "TeslaInfo" => result });
    }

If responseCode is 200, data is a dictionary of two tokens, about 2KB big. If it's not 200, then it's another number, either negative or positive. So why would Bg.exit generate an Unhandled Exception? What should I check for? Can I "try/catch" an .exit statement?

  • If you use a try/catch on Background.exit() I suspect the exception will be ExitDataSizeLimitException.  Per the API Doc:

    Throws:

    • (Background.ExitDataSizeLimitException)  

      Indicates the data provided exceeds the data size limit (approximately 8 KB). If this exception is caught, the process will not exit and should attempt to call Background.exit() again with less data.

  • Is that 8 KB for all the apps running or per app? Is it the same for a watchface (that's what that code is from)? I know that buffered data accumulates until read by the glance app but like I mentioned, it's a watchface so always running. Max size should be about 2 KB, way below that 8 KB if it's per app.

    On that subject, that 32 KB (28 KB usable) for background space on many watches, is that per app or total for all the apps in the Glance 'ring' (for lack of a better term)?

  • It's 8k when your app does a Background.exit()  The data gets serialized so may be larger than you expect.  If it works sometimes, it could be an edge case where you get more data than you expect at times.

    The data doesn't accumlate if the main app hasn't been run.  Whatever you have queued up gets replaced the next time the background runs, unless your app itself added new data to the queue (see "getBackgroundData()")

    The 28k is for your app alone.  If two apps have a background service set to run at the exact same time, one will run to completion, then the second will run.

  • Ok so they in essence do share that 28KB space, but being only one at a time, there is not space congestion, That's good to know, I was afraid to grab that whole 28KB and not let room for other Glance apps to run lol.

    The max I'll put in the background buffer is either two dictionary entries of about 800 bytes each (access and refresh tokens) plus a timestamp entry (expiration interval of the access token), OR five dictionary entries made up for floats, boolean or numbers representing the vehicle's state. I doubt very much that the overhead of these data pushes it over that 8KB limit.

    I also saw another weird one. I got a "Symbol not found" on the last line  of the code below for multiple languages, including French (the one I'm using) and English -> deu, eng, fre, ita, jpn and on six different watch model.

    		var rezStrings = Rez.Strings;
    		var resourceArray;
    
    		var now = Gregorian.info(Time.now(), Time.FORMAT_SHORT);
    
    		var dayOfWeek = now.day_of_week;
    		if (dayOfWeek != mDayOfWeek) {
    			mDayOfWeek = dayOfWeek;
    			
    			resourceArray = [
    				rezStrings.Sun,
    				rezStrings.Mon,
    				rezStrings.Tue,
    				rezStrings.Wed,
    				rezStrings.Thu,
    				rezStrings.Fri,
    				rezStrings.Sat
    				];
    			mDayOfWeekString = Ui.loadResource(resourceArray[mDayOfWeek - 1]).toUpper();
    		}
    

    I don't get an Array out of bound, so it's not because it can't find the corresponding 0-6 day of week in the array but can't find the corresponding string in the resource file. This, should either fail all the time or not at all, so why does it fail, sometimes??? Can you make sense out of this? Because I can't Disappointed