diff --git a/lib/MockDataStoreService/MockGlobalDataStore.lua b/lib/MockDataStoreService/MockGlobalDataStore.lua index 8edc564..b261401 100644 --- a/lib/MockDataStoreService/MockGlobalDataStore.lua +++ b/lib/MockDataStoreService/MockGlobalDataStore.lua @@ -357,7 +357,21 @@ function MockGlobalDataStore:UpdateAsync(key, transformFunction) error("UpdateAsync rejected with error (request was throttled, but throttled queue was full)", 2) end - local value = transformFunction(Utils.deepcopy(self.__data[key])) + local thread = coroutine.create(function() + return transformFunction(Utils.deepcopy(self.__data[key])) + end) + + local ok, value = coroutine.resume(thread) + + if coroutine.status(thread) == "suspended" then + task.spawn(error, "Transfom function error Callbacks cannot yield") + return + end + + if not ok then + task.spawn(error, `Transform function error {value}`) + return + end if value == nil then -- cancel update after remote call Utils.simulateYield() diff --git a/spec/MockDataStoreService/MockGlobalDataStore.spec.lua b/spec/MockDataStoreService/MockGlobalDataStore.spec.lua index 64b4a7a..093eb1f 100644 --- a/spec/MockDataStoreService/MockGlobalDataStore.spec.lua +++ b/spec/MockDataStoreService/MockGlobalDataStore.spec.lua @@ -693,6 +693,43 @@ return function() end) + it("should not throw error to caller when transform function errors", function() + Test.reset() + Test.setStaticBudgets(100) + local MockGlobalDataStore = Test.Service:GetDataStore("Test") + + local ok, value = pcall(function() + return MockGlobalDataStore:UpdateAsync("TestKey1", function() + error("intentional error") + return "new value" + end) + end) + + expect(ok).to.equal(true) + expect(value).never.to.be.ok() + + end) + + it("should not succeed when transform function yields", function() + Test.reset() + Test.setStaticBudgets(100) + local MockGlobalDataStore = Test.Service:GetDataStore("Test") + + MockGlobalDataStore:ImportFromJSON({TestKey1 = "value"}) + + MockGlobalDataStore:UpdateAsync("TestKey1", function() + task.wait() + return "new value" + end) + + task.wait() + + local exported = HttpService:JSONDecode(MockGlobalDataStore:ExportToJSON()) + + expect(exported.TestKey1).to.equal("value") + + end) + it("should consume budgets correctly", function() Test.reset() Test.setStaticBudgets(100)