Codectionary / Developer documentation / Lua

Lua diagnostic stack traces

debug.traceback builds a stack description. Used as an xpcall handler, it captures frames before error unwinding removes useful context.

Syntax

local ok, result = xpcall(work, debug.traceback)

Examples

Lua diagnostic stack traces

Run with standalone Lua 5.4: lua example.lua. These scripts target Lua 5.4 rather than Roblox Luau.

local function work() error("configuration missing") end
local ok, report = xpcall(work, debug.traceback)
print(ok)
print(type(report))
print(report:find("configuration missing", 1, true) ~= nil)
  1. Output: false, string, true. The full trace also includes file names and line numbers that vary with the execution environment.

Best practices

  • Keep full traces in internal diagnostics rather than exposing paths to end users. Embedded hosts may omit the debug library.

Runtime & versions

Lua interpreter or a host application. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Lua reference manuals

Specifications & further reading