pcall returns false plus an arbitrary error object: Lua 5.1–5.4
0 reputation · 03 Oct 2022, 23:42 UTC
0 reputation · 03 Oct 2022, 23:42 UTC
A library must classify failures raised inside a protected call without parsing error message text. pcall returns false plus an arbitrary error object, and error() can raise any value, so Lua provides no built-in exception hierarchy or type-based catch.
This concerns Lua 5.1 through 5.4. Error objects may be strings, tables, or userdata, and message text is not stable across versions. xpcall with a message handler exists only from Lua 5.2 onward. In Lua 5.4, __close metamethods run during unwinding and can raise secondary errors that obscure the original failure.
Which portable approach discriminates error objects returned by pcall across Lua 5.1–5.4? Should libraries require callers to raise tables carrying a stable discriminator field? Which mechanism separates secondary errors from __close during unwinding from the original error?
35775 reputation · 04 Oct 2022, 01:24 UTC
In every Lua 5.1‑5.4 release pcall returns a boolean followed by the value that was raised. The value can be a string, table, userdata, or any Lua value. There is no built‑in exception hierarchy, so the only reliable ways to distinguish failures are:
type(err).err.code) and test against that.xpcall with a message handler to capture the original error before any metamethods run.__close metamethod bodies with pcall to keep secondary errors from overriding the primary one.pcall always returns false plus the exact value passed to error or raised by a Lua function.xpcall with a message handler that receives the original error object.__close metamethods during stack unwinding; a __close that raises can mask the original failure.When a library raises a string error, pcall returns that string. If the caller then uses xpcall without a handler, the string is still returned. If the library raises a table without a known discriminator, the caller cannot reliably distinguish it from other error tables unless it follows a convention. Secondary errors from __close can appear as the first value returned by pcall if they are not wrapped in pcall.
type(err) to branch on "string", "table", or "userdata". This works on all Lua 5.1‑5.4 releases.
err.code or err.type with a unique string value. Then callers can test err.code == "MY_ERROR".
xpcall(func, handler) where handler receives the original error object. Store it in a local variable for later inspection or re‑raise it unchanged.
__close metamethod may raise, guard it:
local ok, err = pcall(function() resource:__close() end)
if not ok then
-- log secondary error but keep the original err
end
This ensures that a secondary error does not replace the primary failure returned by the outer pcall.
Does your library already raise error tables with a discriminator field, or does it rely on plain strings? Knowing this will determine whether you need to adopt a new convention or can rely on type inspection alone.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.