From 5e8d2cd1f37f234403978eb304cb74589f93246f Mon Sep 17 00:00:00 2001 From: Kiko Beats Date: Tue, 31 Dec 2024 18:19:34 +0100 Subject: [PATCH] fix: catch unsupported format errors --- src/index.js | 10 ++++++---- test/error.js | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index e1e1869..7c77932 100644 --- a/src/index.js +++ b/src/index.js @@ -10,14 +10,16 @@ const args = (flags = {}) => dargs(flags, { useEquals: false }).filter(Boolean) const isJSON = (str = '') => str.startsWith('{') const parse = ({ stdout, stderr, ...details }) => { - if (stdout !== undefined && stdout !== '' && stdout !== 'null') { - return isJSON(stdout) ? JSON.parse(stdout) : stdout - } + if (details.exitCode === 0) { return isJSON(stdout) ? JSON.parse(stdout) : stdout } throw Object.assign(new Error(stderr), { stderr, stdout }, details) } const create = binaryPath => { - const fn = (...args) => fn.exec(...args).then(parse).catch(parse) + const fn = (...args) => + fn + .exec(...args) + .then(parse) + .catch(parse) fn.exec = (url, flags, opts) => $(binaryPath, [url].concat(args(flags)), opts) return fn } diff --git a/test/error.js b/test/error.js index 3bb696e..e1587cb 100644 --- a/test/error.js +++ b/test/error.js @@ -6,6 +6,8 @@ const test = require('ava') const youtubedl = require('..') +const isCI = !!process.env.CI + test.serial('catch errors', async t => { await rename(path.resolve('bin/yt-dlp'), path.resolve('bin/_yt-dlp')) t.teardown(() => @@ -38,6 +40,30 @@ test('unsupported URLs', async t => { t.truthy(error.exitCode) } }) +;(isCI ? test.skip : test)('unsupported format', async t => { + t.plan(6) + const url = 'https://www.youtube.com/watch?v=tPEE9ZwTmy0' + try { + await youtubedl(url, { + extractorArgs: 'youtube:player_client=android,web' + }) + } catch (error) { + t.is( + error.message, + [ + 'WARNING: [youtube] tPEE9ZwTmy0: android client https formats require a PO Token which was not provided. They will be skipped as they may yield HTTP Error 403. You can manually pass a PO Token for this client with --extractor-args "youtube:po_token=android+XXX. For more information, refer to https://github.com/yt-dlp/yt-dlp/wiki/Extractors#po-token-guide . To enable these broken formats anyway, pass --extractor-args "youtube:formats=missing_pot"', + 'WARNING: [youtube] tPEE9ZwTmy0: web client https formats require a PO Token which was not provided. They will be skipped as they may yield HTTP Error 403. You can manually pass a PO Token for this client with --extractor-args "youtube:po_token=web+XXX. For more information, refer to https://github.com/yt-dlp/yt-dlp/wiki/Extractors#po-token-guide . To enable these broken formats anyway, pass --extractor-args "youtube:formats=missing_pot"', + 'WARNING: Only images are available for download. use --list-formats to see them', + 'ERROR: [youtube] tPEE9ZwTmy0: Requested format is not available. Use --list-formats for a list of available formats' + ].join('\n') + ) + t.true(error instanceof Error) + t.truthy(error.command) + t.truthy(error.stderr) + t.truthy(error.stdout) + t.truthy(error.exitCode) + } +}) test('video unavailable', async t => { t.plan(4)