From 16df7801a597b6aa4d42a0d37ae402f7fcc58fe6 Mon Sep 17 00:00:00 2001 From: Richard Date: Sun, 9 Jun 2024 21:50:57 -0700 Subject: [PATCH] fix: `try()` uses pcall if passed `finally()` --- src/store.luau | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/store.luau b/src/store.luau index fb371ab..229d0dd 100644 --- a/src/store.luau +++ b/src/store.luau @@ -28,21 +28,34 @@ local function try(callback: (U...) -> T | any, finally: (() -> ())?, . end if coroutine.status(thread) == "suspended" then + local source, line, name = debug.info(callback, "sln") coroutine.close(thread) - error("Yielding is not allowed in atom functions") + error( + "Yielding is not allowed in atom functions. Consider wrapping this code in a Promise or task.defer instead." + .. `\nFunction defined at: {source}:{line}` + .. if name == "" then "" else ` function {name}` + ) elseif not success then - error("An error occurred while running an atom function") + local source, line, name = debug.info(callback, "sln") + error( + "An error occurred while running an atom function" + .. `\nFunction defined at: {source}:{line}` + .. (if name == "" then "" else ` function {name}`) + .. `\nError: {result}` + ) end return result end - local result = callback(...) - - if finally then - finally() + if not finally then + return callback(...) end + local success, result = pcall(callback, ...) + finally() + assert(success, result) + return result end