Skip to content

Commit

Permalink
fix: catch ENOENT
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Jun 24, 2024
1 parent a97acec commit a5aefa4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ const $ = require('tinyspawn')

const constants = require('./constants')

const args = (flags = {}) =>
dargs(flags, { useEquals: false }).filter(Boolean)
const args = (flags = {}) => dargs(flags, { useEquals: false }).filter(Boolean)

const isJSON = (str = '') => str.startsWith('{')

const parse = ({ stdout, stderr, ...details }) => {
if (stdout !== '' && stdout !== 'null') return isJSON(stdout) ? JSON.parse(stdout) : stdout
if (stdout !== undefined && stdout !== '' && stdout !== 'null') { 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
12 changes: 12 additions & 0 deletions test/error.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
'use strict'

const { rename } = require('fs/promises')
const path = require('path')
const test = require('ava')

const youtubedl = require('..')

test.serial('catch errors', async t => {
await rename(path.resolve('bin/yt-dlp'), path.resolve('bin/_yt-dlp'))
t.teardown(() =>
rename(path.resolve('bin/_yt-dlp'), path.resolve('bin/yt-dlp'))
)
const error = await t.throwsAsync(youtubedl(''), { instanceOf: Error })
t.is(error.errno, -2)
t.is(error.code, 'ENOENT')
})

test('no url', async t => {
const error = await t.throwsAsync(youtubedl(''), { instanceOf: Error })
t.is(error.exitCode, 2)
Expand Down

0 comments on commit a5aefa4

Please sign in to comment.