mirror of
https://github.com/littensy/charm.git
synced 2026-10-25 05:11:37 +00:00
fix: try() uses pcall if passed finally()
This commit is contained in:
parent
9965613507
commit
16df7801a5
1 changed files with 19 additions and 6 deletions
|
|
@ -28,21 +28,34 @@ local function try<T, U...>(callback: (U...) -> T | any, finally: (() -> ())?, .
|
||||||
end
|
end
|
||||||
|
|
||||||
if coroutine.status(thread) == "suspended" then
|
if coroutine.status(thread) == "suspended" then
|
||||||
|
local source, line, name = debug.info(callback, "sln")
|
||||||
coroutine.close(thread)
|
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
|
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
|
end
|
||||||
|
|
||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
|
||||||
local result = callback(...)
|
if not finally then
|
||||||
|
return callback(...)
|
||||||
if finally then
|
|
||||||
finally()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local success, result = pcall(callback, ...)
|
||||||
|
finally()
|
||||||
|
assert(success, result)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue