OK so this code:
if (rc ~= 0) then
msg = mc.mcCntlGetErrorString(inst, rc) --Get the returned error string
errorOut(msg)
return --Exit the function
end
local function errorOut(msg)
local inst = mc.mcGetInstance()
mc.mcSpindleSetDirection(inst, mc.MC_SPINDLE_OFF)
mc.mcCntlMacroAlarm(inst, 3, msg)
end
Can be simplified to this:
value, rc = mc.mcGetSomeValueFromAPI
if (not (rc = mc.MERROR_NOERROR) then
errorOut(rc)
end
local function errorOut(rc)
local inst, rc2 = mc.mcGetInstance()
local msg, rc2 = mc.mcCntlGetErrorString(inst, rc)
-- print some error message here
rc2 = mc.mcSpindleSetDirection(inst, mc.MC_SPINDLE_OFF)
rc2 = mc.mcCntlMacroAlarm(inst, 3, msg)
end
Do you see the problem? It's turtles all the way down!
If my safe error handling routine that I only call when the return code from an API call fails depends on API calls that must themselves have their return codes checked (and could fail!) where's the bottom?
I buy into stopping the spindle explicitly via API vice trying to feed the gcode interpreter an M30 or something, and by extension, we should probably stop coolant, mist, and all axis movement too, and setting the ALARM state makes sense... but I have to use the API to do it so I have to test the return codes and around and around we go.