Skip to content

Commit

Permalink
More accurate error messages when failing to set properties (#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
mskfox authored Feb 10, 2024
1 parent 4feb8c8 commit d34839d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/Instances/applyInstanceProps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ local External = require(Package.External)
local cleanup = require(Package.Utility.cleanup)
local xtypeof = require(Package.Utility.xtypeof)
local logError = require(Package.Logging.logError)
local parseError = require(Package.Logging.parseError)
local Observer = require(Package.State.Observer)
local peek = require(Package.State.peek)

Expand All @@ -31,7 +32,9 @@ local function testPropertyAssignable(instance: Instance, property: string)
end

local function setProperty(instance: Instance, property: string, value: any)
if not pcall(setProperty_unsafe, instance, property, value) then
local success, err = xpcall(setProperty_unsafe, parseError, instance, property, value)

if not success then
if not pcall(testPropertyAssignable, instance, property) then
if instance == nil then
-- reference has been lost
Expand All @@ -45,7 +48,12 @@ local function setProperty(instance: Instance, property: string, value: any)
-- this typically implies the wrong type was received
local givenType = typeof(value)
local expectedType = typeof((instance :: any)[property])
logError("invalidPropertyType", nil, instance.ClassName, property, expectedType, givenType)

if givenType == expectedType then
logError("propertySetError", err)
else
logError("invalidPropertyType", nil, instance.ClassName, property, expectedType, givenType)
end
end
end
end
Expand Down
1 change: 1 addition & 0 deletions src/Logging/messages.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ return {
mistypedSpringSpeed = "The speed of a spring must be a number. (got a %s)",
mistypedTweenInfo = "The tween info of a tween must be a TweenInfo. (got a %s)",
noTaskScheduler = "Fusion is not connected to an external task scheduler.",
propertySetError = "Error setting property: ERROR_MESSAGE",
springTypeMismatch = "The type '%s' doesn't match the spring's type '%s'.",
stateGetWasRemoved = "`StateObject:get()` has been replaced by `use()` and `peek()` - see discussion #217 on GitHub.",
strictReadError = "'%s' is not a valid member of '%s'.",
Expand Down

0 comments on commit d34839d

Please sign in to comment.