This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 143
Error boundaries! #173
Open
AmaranthineCodices
wants to merge
8
commits into
Roblox:master
Choose a base branch
from
AmaranthineCodices:error-boundaries
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Error boundaries! #173
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7c5ab41
add and test getDerivedStateFromError
AmaranthineCodices c2d0bca
merge in changes I missed
AmaranthineCodices 74b2f69
oops!
AmaranthineCodices 1830bbd
rename __updateChildren to __updateVirtualNode
AmaranthineCodices bbbb427
rework tests to use spies instead of manual call counting
AmaranthineCodices bbaff86
merge and update to new-reconciler latest
AmaranthineCodices d579a32
reindent (?!)
AmaranthineCodices 6eb5e61
Merge branch 'new-reconciler' into error-boundaries
AmaranthineCodices File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
return function() | ||
local createElement = require(script.Parent.Parent.createElement) | ||
local createReconciler = require(script.Parent.Parent.createReconciler) | ||
local createSpy = require(script.Parent.Parent.createSpy) | ||
local NoopRenderer = require(script.Parent.Parent.NoopRenderer) | ||
|
||
local Component = require(script.Parent.Parent.Component) | ||
|
||
local noopReconciler = createReconciler(NoopRenderer) | ||
|
||
it("should not be called if the component doesn't throw", function() | ||
local Boundary = Component:extend("Boundary") | ||
|
||
local getDerivedSpy = createSpy(function(message) | ||
return {} | ||
end) | ||
|
||
Boundary.getDerivedStateFromError = getDerivedSpy.value | ||
|
||
function Boundary:render() | ||
return nil | ||
end | ||
|
||
local element = createElement(Boundary) | ||
local hostParent = nil | ||
local key = "Test" | ||
|
||
noopReconciler.mountVirtualNode(element, hostParent, key) | ||
expect(getDerivedSpy.callCount).to.equal(0) | ||
end) | ||
|
||
it("should be called with the error message", function() | ||
local function Bug() | ||
error("test error") | ||
end | ||
|
||
local Boundary = Component:extend("Boundary") | ||
|
||
local getDerivedSpy = createSpy(function(message) | ||
-- The error message will not be the same as what's thrown because a | ||
-- line/source object as well as stack trace will be attached to it | ||
expect(message).to.be.ok() | ||
|
||
return {} | ||
end) | ||
|
||
Boundary.getDerivedStateFromError = getDerivedSpy.value | ||
|
||
function Boundary:render() | ||
return createElement(Bug) | ||
end | ||
|
||
local element = createElement(Boundary) | ||
local hostParent = nil | ||
local key = "Test" | ||
|
||
-- This will throw, because we don't stop rendering the throwing | ||
-- component in response to the error. We test this more | ||
-- rigorously elsewhere; this is just to keep the test from failing. | ||
expect(function() | ||
noopReconciler.mountVirtualNode(element, hostParent, key) | ||
end).to.throw() | ||
expect(getDerivedSpy.callCount).to.equal(1) | ||
end) | ||
|
||
it("should throw an error if the fallback render throws", function() | ||
local function Bug() | ||
error("test error") | ||
end | ||
|
||
local Boundary = Component:extend("Boundary") | ||
|
||
function Boundary.getDerivedStateFromError(message) | ||
return {} | ||
end | ||
|
||
local renderSpy = createSpy(function(self) | ||
return createElement(Bug) | ||
end) | ||
|
||
Boundary.render = renderSpy.value | ||
|
||
local element = createElement(Boundary) | ||
local hostParent = nil | ||
local key = "Test" | ||
|
||
expect(function() | ||
noopReconciler.mountVirtualNode(element, hostParent, key) | ||
end).to.throw() | ||
expect(renderSpy.callCount).to.equal(2) | ||
end) | ||
|
||
it("should return a state delta for the component", function() | ||
local getStateCallback = nil | ||
|
||
local function Bug() | ||
error("test error") | ||
end | ||
|
||
local Boundary = Component:extend("Boundary") | ||
|
||
function Boundary.getDerivedStateFromError(message) | ||
return { | ||
errored = true | ||
} | ||
end | ||
|
||
function Boundary:init() | ||
getStateCallback = function() | ||
return self.state | ||
end | ||
end | ||
|
||
local renderSpy | ||
renderSpy = createSpy(function(self) | ||
if renderSpy.callCount > 1 then | ||
expect(self.state.errored).to.equal(true) | ||
end | ||
|
||
if self.state.errored then | ||
return nil | ||
else | ||
return createElement(Bug) | ||
end | ||
end) | ||
|
||
Boundary.render = renderSpy.value | ||
|
||
local element = createElement(Boundary) | ||
local hostParent = nil | ||
local key = "Test" | ||
|
||
noopReconciler.mountVirtualNode(element, hostParent, key) | ||
expect(renderSpy.callCount).to.equal(2) | ||
expect(getStateCallback().errored).to.equal(true) | ||
end) | ||
|
||
it("should not interrupt the lifecycle methods", function() | ||
local setStateCallback = nil | ||
|
||
local function Bug() | ||
error("test error") | ||
end | ||
|
||
local Boundary = Component:extend("Boundary") | ||
|
||
function Boundary.getDerivedStateFromError(message) | ||
return { | ||
errored = true | ||
} | ||
end | ||
|
||
function Boundary:init() | ||
setStateCallback = function(delta) | ||
self:setState(delta) | ||
end | ||
end | ||
|
||
function Boundary:render() | ||
if self.state.errored then | ||
return nil | ||
else | ||
return createElement(Bug) | ||
end | ||
end | ||
|
||
local didMountSpy = createSpy() | ||
Boundary.didMount = didMountSpy.value | ||
|
||
local didUpdateSpy = createSpy() | ||
Boundary.didUpdate = didUpdateSpy.value | ||
|
||
local element = createElement(Boundary) | ||
local hostParent = nil | ||
local key = "Test" | ||
|
||
noopReconciler.mountVirtualNode(element, hostParent, key) | ||
|
||
expect(didMountSpy.callCount).to.equal(1) | ||
expect(didUpdateSpy.callCount).to.equal(0) | ||
|
||
setStateCallback({ | ||
errored = false | ||
}) | ||
|
||
expect(didMountSpy.callCount).to.equal(1) | ||
expect(didUpdateSpy.callCount).to.equal(1) | ||
end) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule lemur
updated
7 files
+0 −2 | FEATURES.md | |
+0 −1 | lib/Signal.lua | |
+0 −1 | lib/createEnvironment.lua | |
+0 −6 | lib/instances/GuiObject.lua | |
+0 −1 | lib/instances/GuiObject_spec.lua | |
+0 −4 | lib/instances/Players.lua | |
+0 −7 | lib/instances/Players_spec.lua |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It just occurred to me that this implementation doesn't actually catch errors if they occur downstream of this call site.
For example, suppose I have:
Consider these scenarios:
ComponentA
renders, causingComponentB
to render.ComponentB
throws an error. TheErrorBound
catches and handles it just fine.ComponentB
has a state update triggered, and re-renders.ComponentB
throws an error. TheErrorBound
won't be involved in reconciling it, so it won't be invoked!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I need to revisit this when I have the time - there are definitely cases that error boundaries should catch, but don't.