Skip to content

Commit

Permalink
fix: catch unsupported format errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Dec 31, 2024
1 parent 7fb8a44 commit 5e8d2cd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
26 changes: 26 additions & 0 deletions test/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() =>
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5e8d2cd

Please sign in to comment.