LSTERR always equals -1

I’m using the code from this post: https://forum.hms-networks.com/t/basic-s…g/67326/13


`ONERROR "GOTO ERROR"


ERROR:  
error_number% = GETSYS PRG, “LSTERR”  
LOGEVENT STR$ error_number%`

So far every error number has been -1. This isn’t very helpful without knowing what -1 means and if LSTERR is always -1, there’s no need to check the value.

Hello,

That value is is for the BASIC IDE error checking. The Description is follows:

Contains the code from the last BASIC error that occurred. If -1 is returned, this means
no error was found.
Check the BASIC Error Codes, p. 108
The LSTERR is automatically cleared (value -1) when an end of section is reached
(instruction END).
LSTERR can be cleared by setting the parameter to -1: SETSYS PRG, “LSTERR”, -1.

Are you experiencing errors with your code?

I’m looking at our existing code base and several of our function use MQTT “PUBLISH”. Along with the error checking overhead, this seem inefficient. I would think it would be better to have one function that publishes and just pass the string of what I want to publish. The only reason I could see NOT to do this would be if the theoretical MQTT_PUBLISH($StuffToPublish$) function would get too busy. For example, if my MQTTAlarmMsgPublish function passes data to MQTT_PUBLISH and before it has finished uploading that to Azure my MQTTAlarmSummaryPublish and my MQTTHistoricalDataCreate functions both pass data to MQTT_PUBLISH, what will happen? Will it spawn 3 processes all publishing data to Azure, or queue them up to send sequentialy, or jst drop/ignore anything that calls it before its finished?

I think I’m seeing errors? I made a tag named CURRENTFUNCTION@ and at the start of each function, I set the tag value equal to the function name like this:

FUNCTION MQTTConnectionCheck()
  CURRENTFUNCTION@="MQTTCONNECTIONCHECK"
  IF MQTT_CONNECTED@=1 THEN
   STATUS% = MQTT "STATUS"
    MQTT_CONNECTION_STATUS@=STATUS%
    IF MQTT_CONNECTION_STATUS@ < 5 THEN
      MQTT_MESSAGE_FAIL@=MQTT_MESSAGE_FAIL@+1
      IF MQTT_DOWN_ALM_ADMIN@=0 THEN
        TSET 4,1500
        MQTT_DOWN_ALM_ADMIN@=1
      ENDIF 
    ENDIF
    IF MQTT_CONNECTION_STATUS@ = 5 THEN
      MQTT_DOWN_ALM_ADMIN@=0
      TSET 4,0
    ENDIF
  ELSE
    MQTT_MESSAGE_FAIL@=MQTT_MESSAGE_FAIL@+1
  ENDIF
  IF MQTT_MESSAGE_FAIL@ >4 THEN
    @MQTTConnectRetry()
  ENDIF
ENDFN

I also use ONERROR “GOTO ERROR” like I mentioned:

ERROR:
  LASTERROR% = GETSYS PRG,"LSTERR"
  LOGEVENT "Error in function (" + CURRENTFUNCTION@ + "): " + STR$ LASTERROR%, 80
  CLOSE 1

And this appears in my events log:

Error in function (INSIGHTHEARTBEATSET): -1
Error in function (MQTTCONNECTIONCHECK): -1
Error in function (MQTTCONNECTIONCHECK): -1
Error in function (VPNENABLE): -1

When does processing get shunted to the error handler? At the end of the function or when the error occurs? That is, suppose the line “TSET 4,1500” in the MQTTConnectionCheck function throws an error. Does processing continue until “ENDFN” before going to “ERROR:” or does it go immediately to “ERROR:”?

Any suggestions? From what I can tell, 1 of 2 things must be true:

  1. LSTERR really is -1 and there was no error but processing is being passed to ERROR: due to a bug.
  2. There is an error but LSTERR is being set to -1 before it reaches ERROR:

Additionally, page 108 shows error codes, but -1 is not on the list of error codes.

I’m also still in the dark about the order of processing. If one function handles (for example) file writes and it gets called by 2 functions at once, what happens? E.G., suppose it is currently writing historical values to a file when an ONTIMER event occurs that also calls for writing data to a file. Is the 2nd event ignored? Do both writes occur simultaneously? Do the writes happen sequentially?