All files / src/utils does-url-exist.js

0% Statements 0/12
0% Branches 0/8
0% Functions 0/1
0% Lines 0/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27                                                     
const axios = require('axios');
const Logger = require('./logger');
 
/**
 * Checks if a URL returns a 200 status code.
 * @param {string} url - The URL to check.
 * @returns {Promise<boolean>} - True if the URL exists (200), false otherwise.
 */
async function doesUrlExist(url) {
    try {
        const response = await axios.head(url, { timeout: 5000 });
        return response.status === 200;
    } catch (error) {
        // Handle specific error cases for better logging/debugging
        if (error.response && error.response.status === 404) {
            Logger.debug('URL does not exist:', url);
        } else if (error.code === 'ENOTFOUND' || error.code === 'ECONNABORTED') {
            Logger.debug('Network issues with URL:', url);
        } else {
            Logger.debug('Failed to check URL:', url, error.message);
        }
        return false;
    }
}
 
module.exports = { doesUrlExist };