From fd8c673cfc1ee51f0bb05c8247a35d44003366da Mon Sep 17 00:00:00 2001 From: Retusthese <84878539+Retusthese@users.noreply.github.com> Date: Thu, 27 May 2021 15:25:48 -0400 Subject: ytdl_hook: improve handling of json parsing errors This moves the JSON parsing above the main youtube-dl error-handling block and integrates parsing errors into that block. Now, if a parsing error occurs, youtube-dl's stderr will be logged as it is with other errors. This also catches errors that cause youtube-dl to output "null", which would previously be mishandled as a parsing error and crash ytdl_hook when it attempted to concatenate the error string from parse_json. --- player/lua/ytdl_hook.lua | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'player/lua') diff --git a/player/lua/ytdl_hook.lua b/player/lua/ytdl_hook.lua index 2c728455b8..b9cb04645e 100644 --- a/player/lua/ytdl_hook.lua +++ b/player/lua/ytdl_hook.lua @@ -793,33 +793,35 @@ function run_ytdl_hook(url) return end - if (es < 0) or (json == nil) or (json == "") then + local parse_err = nil + + if (es < 0) or (json == "") then + json = nil + elseif json then + json, parse_err = utils.parse_json(json) + end + + if (json == nil) then -- trim our stderr to avoid spurious newlines ytdl_err = result.stderr:gsub("^%s*(.-)%s*$", "%1") msg.error(ytdl_err) local err = "youtube-dl failed: " if result.error_string and result.error_string == "init" then err = err .. "not found or not enough permissions" + elseif parse_err then + err = err .. "failed to parse JSON data: " .. parse_err elseif not result.killed_by_us then err = err .. "unexpected error occurred" else err = string.format("%s returned '%d'", err, es) end msg.error(err) - if string.find(ytdl_err, "yt%-dl%.org/bug") then + if parse_err or string.find(ytdl_err, "yt%-dl%.org/bug") then check_version(ytdl.path) end return end - local json, err = utils.parse_json(json) - - if (json == nil) then - msg.error("failed to parse JSON data: " .. err) - check_version(ytdl.path) - return - end - msg.verbose("youtube-dl succeeded!") msg.debug('ytdl parsing took '..os.clock()-start_time..' seconds') -- cgit v1.2.3