Skip to content

Commit

Permalink
fix: catch ENOENT (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Jun 24, 2024
1 parent a97acec commit 215a4c7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ 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)
}

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 215a4c7

Please sign in to comment.