Get true stack trace of an error in lua pcall -
so pcall statements, i've been doing this
local status, err = pcall(fn) if not status print(err) print(debug.stacktrace()) end
this works fine basic stuff issue debug.stacktrace()
returns current relative stack trace, not stack trace of error. if error within fn happened 10 levels down in stack, wouldn't know occurred, pcall block failing. wondering if there way stack trace of pcall , not current stack trace. tried debug.stacktrace(err)
didn't make difference.
you need use xpcall
provide custom function add stacktrace error message. from pil:
frequently, when error happens, want more debug information location error occurred. @ least, want traceback, showing complete stack of calls leading error. when pcall returns error message, destroys part of stack (the part went error point). consequently, if want traceback, must build before pcall returns. that, lua provides xpcall function. besides function called, receives second argument, error handler function. in case of errors, lua calls error handler before stack unwinds, can use debug library gather information wants error.
you may want check patch extends pcall
include stacktrace.
as suggested in comments, can use local ok, res = xpcall(f, debug.traceback, args...)
lua 5.2+ or luajit (with lua 5.2 compatibility turned on) , used patch mentioned above lua 5.1.
Comments
Post a Comment