/**
* Prepares a list of URLs for diagnostics with simple error messages.
* @param {string[]} urls An array of URLs to check.
* @returns {object} An object containing the URL list and error messages.
*/
function prepareURLListForDiagnostics(urls) {
const urlList = urls || []; // Initialize with an empty array if input is null or undefined
const errorMessages = {};
for (const url of urlList) {
try {
// Basic URL validation (you can add more sophisticated checks)
if (typeof url !== 'string' || url.trim() === '') {
errorMessages[url] = "Invalid URL format.";
continue; // Skip to the next URL
}
new URL(url); // Attempt to create a URL object - throws error if invalid
} catch (error) {
errorMessages[url] = `Error: ${error.message}`; // Capture and store the error message
}
}
return { urlList, errorMessages };
}
// Example usage (for testing)
// const urls = ["https://www.google.com", "invalid-url", "https://example.com"];
// const diagnostics = prepareURLListForDiagnostics(urls);
// console.log(diagnostics);
Add your comment