How to get traceback and error message from dead coroutine in lua

Viewed 510

How to get traceback and error message from dead coroutine in lua

like normal tracebacks and error from dead coroutine

1 Answers

Using debug.traceback and some output of coroutine.resume

function coroutine.xpcall(co)
  local output = {coroutine.resume(co)}
  if output[1] == false then
    return false, output[2], debug.traceback(co)
  end
  return table.unpack(output)
end

function func()
  error("Crash")
end

local co = coroutine.create(func)    

print(coroutine.xpcall(co)) --Should print error message + the traceback
Related