pcall()

Lua pcall (protected call), calls a function in protected mode to ‘catch’ exceptions and errors and handle them gracefully.

This means that any error in the function is not propagated, instead pcall catches the resulting information or error produced and returns this with a boolean status code. It is really useful for non-critical errors that can be skipped or corrected.

For example, here we are using pcall to catch the errors returned from the function ProtectedMain() that parses a JSON object. We can use if statements to determine if the call was not successful, and handle any errors appropriately. In this case, either skipping and adding Custom Logging, or raising and error with error() and stopping the component.

function main(Data) local Success, Result = pcall(ProtectedMain, "DataIn") if not Success then -- iguana.log("Skipping error: "..Result) -- error("Fatal error occurred: ".. Result) end end function ProtectedMain(Data) local msg = json.parse{data=Data} return msg end

Â