TCP Exception Handling

Hi,

“Lua does not have exception handling”
-> Actually there are some API meant to handle Lua errors.
If there is a function you’re calling that might produce a Lua errors you want to catch, you might want to use “copcall”.
First load it:

require"coxpcall"

Then use the new global “copcall” function, it has the same API as “pcall” function documented there:
source.sierrawireless.com/resou … 1_3/#pcall

local pcall_status, command_return1, command_return2 = copcall(cmd, cmd_param1, cmd_param2)

(Note: copcall is similar to “standard” pcall but it also handles coroutine use cases).

" can test if there is a TCP connection before"
“I got this error: /tcpc.lua:74: attempt to index upvalue ‘tcp’”
-> I would need tcp variable definition in your code to help you more on that topic.

“At the very least I want the whole application to restart automatically not stay dead as it did.”
-> If your application process ends with a system exit code different from 0, then it will be restarted.

So you may use copcall to catch errors and then call os.exit(-1) to force the restart of your application.
But in your case, the upvalue error look like something that should be able to fix in the app.

You also can use this kind of code to catch your main “thread” termination.
This is to replace/improve the default and simple “sched.loop” call that must already be in your app.

--register to 'die' to properly log exits
local mainthread = sched.run(main)
sched.sigOnce(mainthread, 'die', function(ev, status) log('MYAPP', 'INFO', 'Exiting [%d]', status and 0 or -1) os.exit(status and 0 or -1) end)
sched.loop()

Please note that this supposes that the “main” function will keep running until an error happens.
So if you also use copcall within the main “thread”, you might not jump into that thread termination handler very often , but it remains a good practice anyway.

Let me know if it helps you or if you need more details on any of the above solutions.

Regards,