Basic Script Error Handling

What are the options for handling errors in the basic script?

For example when using MQTT and calling MQTT “STATUS” if MQTT is not connected then this statement will trigger an error.

What would be a graceful way to handle that error?

You could do something like an OnError function to have the code record that the error occurred

We are already doing an OnErrorr to an error function.

How do we log the cause of the error or the error message?

We have a logevent function that you could call at the end of your OnError function. There’s an example below:

So we need to create our own error messages?

How do I know what basic script statement caused the error?

For example if the following lines are all in a function how do I know which one caused the error?

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

// start reboot timer 25 minutes

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@ >5 THEN

@MQTTReconnect()

ENDIF

image001.jpg

image002.jpg

image002.jpg

The easiest way I could think to find out which line is causing the error would be to put a print statement between each line and find out which one caused the error between those print statements

That seems a bit rudimentary when we are looking for random errors.

image001.jpg

image002.jpg

It’s probably not the most efficient way to do this, but at least it would narrow down where the errors are occurring and we can try and debug further from there

So there is not a method where we can use the OnError to move to an error handling function and then print out what the error message would be for the error that triggered?

I guess I’m not really sure what you’re trying to do with this, do you just want the error messages to print to the console? Because error messages should already do that.

I would really like to log the error message to the event log.

I think you might be able to do something like this. But I’m still trying to figure out the correct syntax for this

Function test_function()
error_number% = getsys PRG, “LSTERR”
print error_number%
logevent STR$ error_number%
//Print “made it here”
Endfn

Hi Ted,

I just spoke with Simon about this, we don’t have a way to call the error message string, the closest thing would be to grab the LSTERR number and then you could compare that to the error list

-Tim

Do you have a quick example of that?

image001.jpg

image002.jpg

I think the code I found here would be pretty useful to you since it references error codes that are generated from BASIC and is used with MQTT