From e683e6fc62ef8e65a5dd1b9f64751a7b30d7ab99 Mon Sep 17 00:00:00 2001 From: Tobi Saputra Date: Sat, 25 Mar 2023 00:51:45 +0700 Subject: [PATCH] handle errors --- package.json | 2 +- utils/index.js | 16 ++++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 2a6cbce..6538472 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@tobyg74/tiktok-api-dl", - "version": "1.0.0", + "version": "1.0.1-fix", "description": "Scrapper for download Video, Image, Music from Tiktok", "main": "index.js", "scripts": { diff --git a/utils/index.js b/utils/index.js index 97ca55c..9d77915 100644 --- a/utils/index.js +++ b/utils/index.js @@ -11,17 +11,21 @@ const TiktokDL = (url) => { }) .then(({ request }) => { const { responseUrl } = request.res; - const ID = responseUrl.match(/\d{17,21}/g)[0]; + let ID = responseUrl.match(/\d{17,21}/g) + if (ID === null) return reject({ + status: "error", + message: "Failed to fetch tiktok url. Make sure your tiktok url is correct!", + }); + ID = ID[0] Axios(_tiktokapi(ID), { method: "GET", }) .then(({ data }) => { const content = data.aweme_list.filter((v) => v.aweme_id === ID)[0]; - if (content.aweme_id !== ID) - return resolve({ - status: "error", - message: "Failed to find tiktok data. Make sure your tiktok url is correct!", - }); + if (!content) return resolve({ + status: "error", + message: "Failed to find tiktok data. Make sure your tiktok url is correct!", + }); const statistics = { play_count: content.statistics.play_count, download_count: content.statistics.download_count,