From 942a47330efa1b08ea5ec3069d8a70c9d0729e04 Mon Sep 17 00:00:00 2001 From: Tangent Date: Sat, 9 Nov 2024 00:48:44 -0700 Subject: [PATCH] fix error by which utility.open might not always close file handles --- utility-functions.lua | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/utility-functions.lua b/utility-functions.lua index c2de14e..4cf292a 100644 --- a/utility-functions.lua +++ b/utility-functions.lua @@ -133,9 +133,13 @@ utility.open = function(file_name, mode, custom_error_message) local file, err = io.open(file_name, mode) if not file then error(custom_error_message or err) end return function(fn) - local result = fn(file) + local success, result_or_error = pcall(function() return fn(file) end) file:close() - return result + if success then + return result_or_error + else + error(result_or_error) + end end end