Skip to main content

pcall()

Call a function in protected mode: an error is returned instead of stopping the macro.

Signature​

pcall(fn: function): boolean, any

Parameters​

ParameterTypeDescription
fnfunctionThe function to call

Returns

boolean, any - true and the function's result on success; false and the error message on failure.

Description​

The builder has three "try / catch error" blocks that generate pcall():

  • try / catch error runs the catch section when the try section fails.
  • try / catch error with result evaluates to the success flag and the error message.
  • try / catch error as variable stores the error message in a variable.

This is useful around network calls, JSON parsing, or anything that may fail because of content you do not control.

Example​

local ok, result = pcall(function()
return json.decode('{"name":"John"}')
end)

if ok then
toast("Hello " .. result.name)
else
toast("Bad JSON: " .. result)
end