Skip to content

Commit

Permalink
Add fix for broken test
Browse files Browse the repository at this point in the history
  • Loading branch information
ZoteTheMighty committed Oct 7, 2022
1 parent 6758f69 commit d5460c4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/NoYield.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@

local function resultHandler(co: thread, ok: boolean, ...)
if not ok then
local message = (...)
error(debug.traceback(co, message), 2)
local err = (...)
if typeof(err) == "string" then
error(debug.traceback(co, err), 2)
else
-- If the error is not of type string, just assume it has some
-- meaningful information and rethrow it with a `tostring` so that
-- top-level error handlers can process it
error(tostring(err), 2)
end
end

if coroutine.status(co) ~= "dead" then
Expand Down
26 changes: 26 additions & 0 deletions src/NoYield.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,30 @@ return function()
expect(err:find("foo")).to.be.ok()
expect(err:find("NoYield.spec")).to.be.ok()
end)

it("should handle non-string error messages", function()
local count = 0

local function makeErrorObject()
return setmetatable({
message = "errored with an error object",
stack = debug.traceback(),
}, {
__tostring = function(self)
return self.message .. "\n" .. self.stack
end,
})
end

local function test()
count = count + 1
error(makeErrorObject())
end

local ok, err = pcall(NoYield, test)

expect(ok).to.equal(false)
expect(err:find("errored with an error object")).to.be.ok()
expect(err:find("NoYield.spec")).to.be.ok()
end)
end

0 comments on commit d5460c4

Please sign in to comment.