Prevent a channel from stopping on errors

Verified
Added by iNTERFACEWARE

A very simple way to run all your code in protected mode with pcall() and log errors instead of stopping

Source Code
local function ProtectedMain(Data)
   
   -- replace with real processing and real error handling
   error("Bang!")
   
   return "Some", "Data"
end

function main(Data)   
   local Success, Err = pcall(ProtectedMain, "DataIn")
   if not Success then
      iguana.logError("Skipping error: "..Err)
   end
end
Description
A very simple way to run all your code in protected mode with pcall() and log errors instead of stopping
Usage Details

This code uses pcall() to run all your code in protected mode, and log errors instead of stopping. The principle is very simple we simply replace main() with protectedMain(). Then we call pcall() to run protectedMain() , from main(), and trap and log all errors that occur.

Note: The protectedMain() only contains dummy code to raise an error.

How to use the code:

  • Paste the code into a script
  • Inspect the code, annotations and logs to see how it works
  • To protect an existing main() is very simple:
    • Rename (existing) main() to protectedMain()
    • Use the main() from the above code
    • You may want to adapt the error handling in main()