You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Error message from pcall only include the first line that error happened but not the full stack traceback, but this can be done by compiling to using xpcall and pass debug.traceback as second arg:
local result
local function ____catch(err)
print("catch:", err)
end
local ____try, ____hasReturned = xpcall(
function()
result = dosomething(args)
end,
debug.traceback
)
if not ____try then
____catch(____hasReturned)
end
Although custom plugin can implement this feature, but plugin can not rewrite lualib such as pcall in Promise.lua
Unfortunately, it is not as simple as replacing pcall with xpcall & debug.traceback.
Because using xpcall & debug.traceback will break our throw implementation.
e.g.
try {
throw "Hello, I am an Error!";
} catch (error) {
return error;
}
would return:
Hello, I am an Error!
stack traceback:
[C]: in function 'error'
[string \"...\"]:17: in function <[string \"...\"]:16>
[C]: in function 'xpcall'
[string \"...\"]:15: in function <[string \"...\"]:6>
(...tail calls...)
[string \"...\"]:3: in main chunk
instead of the correct/JS behavior
Hello, I am an Error!
This can possibly be solved by changing the way we throw string literals. Another solution might be to create a custom/adapted implementation of debug.tracepack.
Likely the best middle ground is that an empty catch block can compile to an xpcall, and a non-empty one can compile as-is. Same with un-caught rejected promise objects, if they don't already work that way.
coroutine.resume also lack of stack trace, the asyncCoroutine should pass into debug.traceback as the first argument to get the error trace in coroutine context.
-- Reimplemented pcall function_G.pcall=function(func, ...)
localresults=table.pack(
xpcall(func, function(err)
-- Do nothing here, just return the error objectreturnerr..'\n'..debug.traceback()
end, ...)
)
localstatus=table.remove(results, 1)
ifnotstatusthen-- When an error occurs, err is the error message, mimicking the behavior of pcallreturnfalse, table.unpack(results)
else-- No error occurred, return all the additional parameters returned by xpcallreturntrue, table.unpack(results)
endend
We can use xpcall to override pcall method to find the origin trackback
Activity
lolleko commentedon Sep 10, 2023
Unfortunately, it is not as simple as replacing
pcallwithxpcall&debug.traceback.Because using
xpcall&debug.tracebackwill break ourthrowimplementation.e.g.
would return:
instead of the correct/JS behavior
This can possibly be solved by changing the way we throw string literals. Another solution might be to create a custom/adapted implementation of debug.tracepack.
BribeFromTheHive commentedon Feb 9, 2024
Likely the best middle ground is that an empty catch block can compile to an xpcall, and a non-empty one can compile as-is. Same with un-caught rejected promise objects, if they don't already work that way.
pilaoda commentedon Mar 27, 2024
TypeScriptToLua/src/lualib/Await.ts
Lines 55 to 58 in 16a8eb3
coroutine.resumealso lack of stack trace, theasyncCoroutineshould pass intodebug.tracebackas the first argument to get the error trace in coroutine context.fadabaerobe6 commentedon Apr 27, 2024
We can use
xpcallto overridepcallmethod to find the origin trackback