From 1a68875aada064964786b7249665be56df66ddc3 Mon Sep 17 00:00:00 2001 From: LuanRT Date: Fri, 15 Oct 2021 00:04:39 -0300 Subject: [PATCH] fix: add some better error handling --- examples/index.js | 7 +++++-- lib/Innertube.js | 5 +++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/examples/index.js b/examples/index.js index edc73183..bf82de5d 100644 --- a/examples/index.js +++ b/examples/index.js @@ -1,3 +1,5 @@ +'use strict'; + const fs = require('fs'); const Innertube = require('..'); @@ -10,7 +12,8 @@ async function start() { const video = await youtube.getDetails(search.videos[0].id); console.info('Video details:', video); - + if (video.error) return; + if (youtube.logged_in) { const myNotifications = await youtube.getNotifications(); console.info('My notifications:', myNotifications); @@ -53,7 +56,7 @@ async function start() { type: 'videoandaudio' // can be “video”, “audio” and “videoandaudio” }); - stream.pipe(fs.createWriteStream(`./${search.videos[0].title}.mp4`)); + stream.pipe(fs.createWriteStream(`./${search.videos[0].id}.mp4`)); stream.on('start', () => { console.info('[DOWNLOADER]', 'Starting download now!'); diff --git a/lib/Innertube.js b/lib/Innertube.js index a27ceaf7..7c7306b0 100644 --- a/lib/Innertube.js +++ b/lib/Innertube.js @@ -91,6 +91,7 @@ class Innertube extends EventEmitter { } async search(query, options = { period: 'any', order: 'relevance', duration: 'any' }) { + if (!query) throw new Error('No query was provided'); if (!this.initialized) throw new Error('Missing Innertube data.'); const yt_response = await Axios.post(`${Constants.urls.YT_BASE_URL}/youtubei/v1/search${this.logged_in && this.cookie.length < 1 ? '' : `?key=${this.key}`}`, JSON.stringify({ context: this.context, params: Constants.filters(options.period + ',' + options.duration + ',' + options.order), query }), Constants.innertube_request_opts({ session: this })).catch((error) => error); @@ -135,6 +136,8 @@ class Innertube extends EventEmitter { } async getDetails(id) { + if (!id) return { error: 'Video id missing' }; + const data = await this.requestVideoInfo(id, false); const video_data = Constants.formatVideoData(data, this, false); @@ -192,6 +195,8 @@ class Innertube extends EventEmitter { } download(id, options = {}) { + if (!id) throw new Error('Video id missing'); + options.quality = options.quality || '360p'; options.type = options.type || 'videoandaudio'; options.format = options.format || 'mp4';