diff --git a/lib/init.lua b/lib/init.lua index 144469e..e69e666 100644 --- a/lib/init.lua +++ b/lib/init.lua @@ -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 --[[ diff --git a/lib/init.spec.lua b/lib/init.spec.lua index 3ce1811..847af1c 100644 --- a/lib/init.spec.lua +++ b/lib/init.spec.lua @@ -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)