diff --git a/lib/info.js b/lib/info.js index 68c9143b..8531db1e 100644 --- a/lib/info.js +++ b/lib/info.js @@ -58,24 +58,24 @@ exports.getBasicInfo = async(id, options) => { let getWatchPage = async(maxRetries = 1) => { let body = await miniget(jsonEndpointURL, reqOptions).text(); - let info; + let parsedBody; try { let jsonClosingChars = /^[)\]}'\s]+/; if (jsonClosingChars.test(body)) { body = body.replace(jsonClosingChars, ''); } - let parsedBody = JSON.parse(body); + parsedBody = JSON.parse(body); if (parsedBody.reload === 'now' && maxRetries > 0) { await setIdentityToken('browser', false); return getWatchPage(maxRetries - 1); } - if (!Array.isArray(parsedBody)) { - throw Error('Unable to retrieve video metadata'); - } - info = parsedBody.reduce((part, curr) => Object.assign(curr, part), {}); } catch (err) { throw Error(`Error parsing info: ${err.message}`); } + if (!Array.isArray(parsedBody)) { + throw Error('Unable to retrieve video metadata'); + } + let info = parsedBody.reduce((part, curr) => Object.assign(curr, part), {}); return [info, body]; }; diff --git a/test/info-test.js b/test/info-test.js index 2b163e8e..25f95727 100644 --- a/test/info-test.js +++ b/test/info-test.js @@ -406,7 +406,7 @@ describe('ytdl.getInfo()', () => { type: 'with-cookie', watch: 'reload-now', }); - await assert.rejects(ytdl.getInfo(id), /Unable to retrieve video metadata/); + await assert.rejects(ytdl.getInfo(id), /Error: Unable to retrieve video metadata/); scope.done(); scope2.done(); });