natural-harmonia-gropius / recent-menu

Recently played menu for mpv-player.
MIT License
33 stars 2 forks source link

Crash if file not exists #7

Closed natural-harmonia-gropius closed 1 year ago

natural-harmonia-gropius commented 1 year ago

and check what will happen if json is empty and no json

dyphire commented 1 year ago

json文件不存在时在测试中没有出现崩溃,代码中也没看出来问题,是有什么边缘案例吗? json文件为空时会出现报错,以下补丁可以解决:

---
 recentmenu.lua | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/recentmenu.lua b/recentmenu.lua
index 2f40d46..365f08c 100644
--- a/recentmenu.lua
+++ b/recentmenu.lua
@@ -26,7 +26,9 @@ function read_json()
     local json = json_file:read("a")
     json_file:close()

-    menu.items = utils.parse_json(json)
+    local contents = utils.parse_json(json)
+    if not contents then return end
+    menu.items = contents
 end

 function write_json()
-- 
natural-harmonia-gropius commented 1 year ago

错误的 json 正常使用也不会有,这个不管。 如果路径不存在 mpv 会闪退,但 https://www.bilibili.com/video/BV1 也没法检测,这个也不管了。