lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I noticed there doesn't seem to be any way to propagate errors "caught" with pcall/xpcall/lua_pcall. Why is this the case?

Throwing an error from the error handler isn't an option, as Lua eats those, and calling error() isn't an option as Lua eats the backtrace in that case. It would be nice to see a return value used to indicate whether to "keep unwinding" or just eat the error.[1][2]

Nested pcalls with "keep unwinding" set would give access to the full backtrace to all pcalls. That is, if you have 3 propagating pcalls then 1 eating pcall, they'd all have their error handlers called before the error unwinds the stack. Returning a new error object would use the new error object for future error handler calls. Error handlers would be called in reverse order. Erroring in non-propagating error handlers would just, uh, so like there are 2 ways to do this: restart the error handling from the top of the stack, or restart from the erroring error handler. I'm not sure which one would be "more correct" but I'd probably go with the latter.

Other languages don't do this - they print errors as soon as their equivalent of error() is called.[3]

[1] https://www.lua.org/manual/5.3/manual.html#2.3

[2] https://www.lua.org/manual/5.3/manual.html#lua_pcall

[3] https://doc.rust-lang.org/std/panic/fn.set_hook.html