Skip to content

Commit a4c4c52

Browse files
committed
fix: catch unsupported format errors
1 parent 7fb8a44 commit a4c4c52

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/index.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ const args = (flags = {}) => dargs(flags, { useEquals: false }).filter(Boolean)
1010
const isJSON = (str = '') => str.startsWith('{')
1111

1212
const parse = ({ stdout, stderr, ...details }) => {
13-
if (stdout !== undefined && stdout !== '' && stdout !== 'null') {
14-
return isJSON(stdout) ? JSON.parse(stdout) : stdout
15-
}
13+
if (details.exitCode === 0) { return isJSON(stdout) ? JSON.parse(stdout) : stdout }
1614
throw Object.assign(new Error(stderr), { stderr, stdout }, details)
1715
}
1816

1917
const create = binaryPath => {
20-
const fn = (...args) => fn.exec(...args).then(parse).catch(parse)
18+
const fn = (...args) =>
19+
fn
20+
.exec(...args)
21+
.then(parse)
22+
.catch(parse)
2123
fn.exec = (url, flags, opts) => $(binaryPath, [url].concat(args(flags)), opts)
2224
return fn
2325
}

test/error.js

+25
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,31 @@ test('unsupported URLs', async t => {
3939
}
4040
})
4141

42+
test('unsupported format', async t => {
43+
t.plan(6)
44+
const url = 'https://www.youtube.com/watch?v=tPEE9ZwTmy0'
45+
try {
46+
await youtubedl(url, {
47+
extractorArgs: 'youtube:player_client=android,web'
48+
})
49+
} catch (error) {
50+
t.is(
51+
error.message,
52+
[
53+
'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"',
54+
'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"',
55+
'WARNING: Only images are available for download. use --list-formats to see them',
56+
'ERROR: [youtube] tPEE9ZwTmy0: Requested format is not available. Use --list-formats for a list of available formats'
57+
].join('\n')
58+
)
59+
t.true(error instanceof Error)
60+
t.truthy(error.command)
61+
t.truthy(error.stderr)
62+
t.truthy(error.stdout)
63+
t.truthy(error.exitCode)
64+
}
65+
})
66+
4267
test('video unavailable', async t => {
4368
t.plan(4)
4469
const url = 'https://www.youtube.com/watch?v=x8erEF_1POY'

0 commit comments

Comments
 (0)