Skip to content

Commit

Permalink
fix: better error message when unable to get video metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
fent committed Oct 28, 2020
1 parent c529063 commit 3b31fe5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
12 changes: 6 additions & 6 deletions lib/info.js
Expand Up @@ -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];
};

Expand Down
2 changes: 1 addition & 1 deletion test/info-test.js
Expand Up @@ -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();
});
Expand Down

0 comments on commit 3b31fe5

Please sign in to comment.