From bf71cf062ca7c8afa8fd3c6264d5f6d379aa3649 Mon Sep 17 00:00:00 2001 From: MillenniumEarl Date: Tue, 15 Dec 2020 09:47:40 +0100 Subject: [PATCH] Reduce cyclomatic complexity --- app/scripts/network-helper.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/app/scripts/network-helper.js b/app/scripts/network-helper.js index 6d17630..35032b3 100644 --- a/app/scripts/network-helper.js +++ b/app/scripts/network-helper.js @@ -242,6 +242,24 @@ module.exports.isStringAValidURL = function (url) { else return false; }; +/** + * @private + * Check with Axios if a URL exists. + * @param {String} url + */ +async function _axiosUrlExists(url) { + // Local variables + let valid = false; + try { + const response = await axios.head(url); + valid = response && !/4\d\d/.test(response.status); + } catch (error) { + if (error.code === "ENOTFOUND") valid = false; + else throw error; + } + return valid; +} + /** * @protected * Check if a particular URL is valid and reachable on the web. @@ -256,8 +274,7 @@ module.exports.urlExists = async function (url, checkRedirect = false) { let valid = false; if (exports.isStringAValidURL(url)) { - const response = await axios.head(url); - valid = response && !/4\d\d/.test(response.status); + valid = await _axiosUrlExists(url); if (valid && checkRedirect) { const redirectUrl = await exports.getUrlRedirect(url);