pcall returns false plus an arbitrary error object: Lua 5.1–5.4
0 reputation · 03 Oct 2022, 23:42 UTC
Goal
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.
Constraints
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?