Skip to content

Commit 8c8da37

Browse files
Adds support for non-string error objects to be caught by NoYield (#79)
* Add fix for broken test * bump tools, address changes
1 parent 6758f69 commit 8c8da37

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

foreman.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tools]
22
rojo = { source = "rojo-rbx/rojo", version = "7.2.0" }
3-
selene = { source = "Kampfkarren/selene", version = "0.18.1" }
4-
stylua = { source = "JohnnyMorganz/StyLua", version = "0.13.1" }
3+
selene = { source = "Kampfkarren/selene", version = "0.21.1" }
4+
stylua = { source = "JohnnyMorganz/StyLua", version = "0.15.1" }
55
luau-lsp = { source = "JohnnyMorganz/luau-lsp", version = "*" }
66
wally = { source = "UpliftGames/wally", version = "0.3.1" }

src/NoYield.lua

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,15 @@
1010

1111
local function resultHandler(co: thread, ok: boolean, ...)
1212
if not ok then
13-
local message = (...)
14-
error(debug.traceback(co, message), 2)
13+
local err = (...)
14+
if typeof(err) == "string" then
15+
error(debug.traceback(co, err), 2)
16+
else
17+
-- If the error is not of type string, just assume it has some
18+
-- meaningful information and rethrow it with a `tostring` so that
19+
-- top-level error handlers can process it
20+
error(tostring(err), 2)
21+
end
1522
end
1623

1724
if coroutine.status(co) ~= "dead" then

src/NoYield.spec.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,30 @@ return function()
5353
expect(err:find("foo")).to.be.ok()
5454
expect(err:find("NoYield.spec")).to.be.ok()
5555
end)
56+
57+
it("should handle non-string error messages", function()
58+
local count = 0
59+
60+
local function makeErrorObject()
61+
return setmetatable({
62+
message = "errored with an error object",
63+
stack = debug.traceback(),
64+
}, {
65+
__tostring = function(self)
66+
return self.message .. "\n" .. self.stack
67+
end,
68+
})
69+
end
70+
71+
local function test()
72+
count = count + 1
73+
error(makeErrorObject())
74+
end
75+
76+
local ok, err = pcall(NoYield, test)
77+
78+
expect(ok).to.equal(false)
79+
expect(err:find("errored with an error object")).to.be.ok()
80+
expect(err:find("NoYield.spec")).to.be.ok()
81+
end)
5682
end

src/makeActionCreator.lua

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ local actions = require(script.Parent.types.actions)
77

88
export type ActionCreator<Type, Action, Args...> = actions.ActionCreator<Type, Action, Args...>
99

10-
local function makeActionCreator<Type, Action, Args...>(
11-
name: Type,
12-
fn: (Args...) -> Action
13-
): ActionCreator<Type, Action, Args...>
10+
local function makeActionCreator<Type, Action, Args...>(name: Type, fn: (Args...) -> Action): ActionCreator<Type, Action, Args...>
1411
assert(type(name) == "string", "Bad argument #1: Expected a string name for the action creator")
1512

1613
assert(type(fn) == "function", "Bad argument #2: Expected a function that creates action objects")

0 commit comments

Comments
 (0)