Best practice for Lua run-time errors: error(), assert(), or send_text()?

In Lua scripts, should run-time errors be handled with assert()/error() or with gcs:send_text() and a nil (or false) function return value?

I’ve been perusing both the example Lua code in the ArduPilot tree and @Yuri_Rage’s ArduMower repository, and both approaches are taken – sometimes both in the same script.

I believe assert()/error() just ends the script while gcs:send_text() other could allow for follow-up action by the user, but in practice the result might be the same, ending the mission and fixing the problem.

assert and error are basically the same.
EG:

assert(foo,"bar")

is the same as:

if not foo then
    error("bar")
end

Both assert and error will terminate your script with a error (unless your using a protected call)

send_text just sends a message to the user but does not terminate the script. As you say you could:

if not foo then
    send_text(0,"bar")
    return
end

In that case you terminate the script without a error.

The difference between terminating with and without a error is that terminating with a error will trigger AP’s error string handling, that will re-print the error message periodically making it harder to miss and also trigger a pre-arm warning.

1 Like