Use xpcall instead of pcall to create better stack traces

This commit is contained in:
Lucien Greathouse 2018-05-21 14:10:38 -07:00
parent 7fb09d103f
commit 8828d467c4
2 changed files with 20 additions and 12 deletions

View file

@ -4,21 +4,24 @@
local PROMISE_DEBUG = false
-- If promise debugging is on, use a version of pcall that warns on failure.
-- This is useful for finding errors that happen within Promise itself.
local wpcall
if PROMISE_DEBUG then
wpcall = function(f, ...)
local result = { pcall(f, ...) }
local function wpcall(f, ...)
local args = {...}
local argCount = select("#", ...)
if not result[1] then
warn(result[2])
end
local result = {
xpcall(function()
return f(unpack(args, 1, argCount))
end, debug.traceback)
}
return unpack(result)
-- If promise debugging is on, warn whenever a pcall fails.
-- This is useful for debugging issues within the Promise implementation
-- itself.
if PROMISE_DEBUG and not result[1] then
warn(result[2])
end
else
wpcall = pcall
return unpack(result)
end
--[[

View file

@ -65,6 +65,11 @@ return function()
expect(callCount).to.equal(1)
expect(promise._status).to.equal(Promise.Status.Rejected)
expect(promise._value[1]:find("hahah")).to.be.ok()
-- Loosely check for the pieces of the stack trace we expect
expect(promise._value[1]:find("init.spec")).to.be.ok()
expect(promise._value[1]:find("new")).to.be.ok()
expect(promise._value[1]:find("error")).to.be.ok()
end)
end)