Improve error reporting

This commit is contained in:
aaron 2024-07-15 17:37:26 +01:00
parent 14f8d38a35
commit 7bae2517cd
7 changed files with 50 additions and 119 deletions

View file

@ -1,35 +1,12 @@
if not game then script = require "test/relative-string" end
local trace = require(script.Parent.trace)
local flags = require(script.Parent.flags)
local graph = require(script.Parent.graph)
type Node<T> = graph.Node<T>
local create_node = graph.create_node
local assert_stable_scope = graph.assert_stable_scope
local evaluate_node = graph.evaluate_node
function create_binding<T>(updater: (T) -> T, binding: T)
if flags.strict then
-- track bind creation trace
local fn = updater
local bind_trace = debug.traceback(nil, trace()-1)
updater = function(...)
local ok, result = xpcall(fn, function(err: string)
return err
end, ...)
if not ok then
local btype =
if (binding :: any).property then (binding :: any).property
elseif (binding :: any).parent then "Parent"
else "children"
error(`PROPERTY BINDING ERROR: Property {btype}\n{result}\nBIND CREATION TRACE:\n{bind_trace}`, 0)
end
return result
end
end
function create_implicit_effect<T>(updater: (T) -> T, binding: T)
evaluate_node(create_node(assert_stable_scope(), updater, binding))
end
@ -39,7 +16,7 @@ type PropertyBinding = {
source: () -> unknown
}
local function update_property(p: PropertyBinding)
local function update_property_effect(p: PropertyBinding)
(p.instance :: any)[p.property] = p.source()
return p
end
@ -49,7 +26,7 @@ type ParentBinding = {
parent: () -> Instance
}
local function update_parent(p: ParentBinding)
local function update_parent_effect(p: ParentBinding)
p.instance.Parent = p.parent()
return p
end
@ -61,7 +38,7 @@ type ChildrenBinding = {
children: () -> Instance | { Instance }
}
local function update_children(p: ChildrenBinding)
local function update_children_effect(p: ChildrenBinding)
local cur_children_set: { [Instance]: true } = p.cur_children_set -- cache of all children parented before update
local new_child_set: { [Instance]: true } = p.new_children_set -- cache of all children parented after update
@ -94,7 +71,7 @@ end
return {
property = function(instance, property, source)
return create_binding(update_property, {
return create_implicit_effect(update_property_effect, {
instance = instance,
property = property,
source = source
@ -102,14 +79,14 @@ return {
end,
parent = function(instance, parent)
return create_binding(update_parent, {
return create_implicit_effect(update_parent_effect, {
instance = instance,
parent = parent
})
end,
children = function(instance, children)
return create_binding(update_children, {
return create_implicit_effect(update_children_effect, {
instance = instance,
cur_children_set = {},
new_children_set = {},