Runtime Errors in Coroutines Are Not Displayed
SuzannaLinn Resident
When a runtime error occurs inside a coroutine the script fails silently, the error is not shown in the Script Warning/Error window (or anywhere else).
Tested with different runtime errors, none is displayed. For instance:
function test()
print("before error")
_()
print("after error")
end
coroutine.resume(coroutine.create(test))
-- > before error
-- ! No error message displayed !!!
In the main thread it works as expected:
function test()
print("before error")
_()
print("after error")
end
test()
-- > before error
-- > attempt to call a nil value (in the Script Warning/Error window)
Log In
SuzannaLinn Resident
Which happens to be as expected.
As
Jessicatz Fairymeadow
has explained:It's a Lua feature, any error in a coroutine is not printed anywhere, instead it's returned from the coroutine.resume call, so one has to check that value.
The right way to write the script is:
function test()
print("before error")
_()
print("after error")
end
ok, errorMessage = coroutine.resume(coroutine.create(test))
if not ok then
print(errorMessage)
end
-- > before call
-- > lua_script:3: attempt to call a nil value
It works similarly to
pcall()
.