Skip to content

Commit

Permalink
Merge a796265 into f745756
Browse files Browse the repository at this point in the history
  • Loading branch information
matthargett committed Nov 2, 2021
2 parents f745756 + a796265 commit 9d05f9f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
18 changes: 16 additions & 2 deletions src/Store.lua
Expand Up @@ -63,8 +63,15 @@ function Store.new(reducer, initialState, middlewares, errorReporter)
self._state = reducer(initialState, initAction)
end, tracebackReporter)
if not ok then
-- selene: allow(incorrect_standard_library_use)
local reducerName = typeof(reducer) == "function" and (debug.info and debug.info(reducer, "n"))
or (debug.getinfo and debug.getinfo(reducer, "n").name)

if reducerName == nil or reducerName == "" then
reducerName = tostring(reducer)
end
self._errorReporter.reportReducerError(initialState, initAction, {
message = "Caught error in reducer with init",
message = "Caught error in reducer (" .. reducerName .. ") with init",
thrownValue = result,
})
self._state = initialState
Expand Down Expand Up @@ -153,8 +160,15 @@ function Store:dispatch(action)
self._isDispatching = false

if not ok then
-- selene: allow(incorrect_standard_library_use)
local reducerName = typeof(self._reducer) == "function" and (debug.info and debug.info(self._reducer, "n"))
or (debug.getinfo and debug.getinfo(self._reducer, "n").name)
if reducerName == nil or reducerName == "" then
reducerName = tostring(self._reducer)
end

self._errorReporter.reportReducerError(self._state, action, {
message = "Caught error in reducer",
message = "Caught error in reducer (" .. reducerName .. ")",
thrownValue = result,
})
end
Expand Down
6 changes: 3 additions & 3 deletions src/Store.spec.lua
Expand Up @@ -168,7 +168,7 @@ return function()

expect(caughtState.Value).to.equal(1)
expect(caughtAction.type).to.equal("@@INIT")
expect(caughtErrorResult.message).to.equal("Caught error in reducer with init")
expect(string.find(caughtErrorResult.message, "Caught error in reducer ")).to.be.ok()
expect(string.find(caughtErrorResult.thrownValue, innerErrorMessage)).to.be.ok()
-- We want to verify that this is a stacktrace without caring too
-- much about the format, so we look for the stack frame associated
Expand All @@ -192,7 +192,7 @@ return function()
}

local innerErrorMessage = "Z4PH0D"
local reducerThatErrorsAfterInit = function(state, action)
local function reducerThatErrorsAfterInit(state, action)
if action.type == "ThrowError" then
error(innerErrorMessage)
elseif action.type == "Increment" then
Expand All @@ -217,7 +217,7 @@ return function()

expect(caughtState.Value).to.equal(2)
expect(caughtAction.type).to.equal("ThrowError")
expect(caughtErrorResult.message).to.equal("Caught error in reducer")
expect(string.find(caughtErrorResult.message, "Caught error in reducer ")).to.be.ok()
expect(string.find(caughtErrorResult.thrownValue, innerErrorMessage)).to.be.ok()
-- We want to verify that this is a stacktrace without caring too
-- much about the format, so we look for the stack frame associated
Expand Down
9 changes: 8 additions & 1 deletion src/thunkMiddleware.lua
Expand Up @@ -16,9 +16,16 @@ local function thunkMiddleware(nextDispatch, store)
end, tracebackReporter)

if not ok then
-- selene: allow(incorrect_standard_library_use)
local reducerName = typeof(action) == "function" and (debug.info and debug.info(action, "n"))
or (debug.getinfo and debug.getinfo(action, "n").name)
if reducerName == nil or reducerName == "" then
reducerName = tostring(action)
end

-- report the error and move on so it's non-fatal app
store._errorReporter.reportReducerError(store:getState(), action, {
message = "Caught error in thunk",
message = "Caught error in thunk (" .. reducerName .. ")",
thrownValue = result,
})
return nil
Expand Down
10 changes: 5 additions & 5 deletions src/thunkMiddleware.spec.lua
Expand Up @@ -75,15 +75,15 @@ return function()
}, { thunkMiddleware }, errorReporter)

local innerErrorMessage = "thunk failed"
local function thunk(_store)
local function failingThunk(_store)
error(innerErrorMessage)
end

store:dispatch(thunk)
store:dispatch(failingThunk)

expect(caughtState.Value).to.equal(1)
expect(caughtAction).to.equal(thunk)
expect(caughtErrorResult.message).to.equal("Caught error in thunk")
expect(caughtAction).to.equal(failingThunk)
expect(string.find(caughtErrorResult.message, "Caught error in thunk ")).to.be.ok()
end)

it("should recover and continue to update after a thunk errors", function()
Expand Down Expand Up @@ -112,7 +112,7 @@ return function()
end

store:dispatch(errorThunk)
expect(caughtErrorResult.message).to.equal("Caught error in thunk")
expect(string.find(caughtErrorResult.message, "Caught error in thunk ")).to.be.ok()

store:dispatch(safeThunk)
expect(ranSafeThunk).to.equal(true)
Expand Down

0 comments on commit 9d05f9f

Please sign in to comment.