1. /**
  2. * Prepares a list of URLs for diagnostics with simple error messages.
  3. * @param {string[]} urls An array of URLs to check.
  4. * @returns {object} An object containing the URL list and error messages.
  5. */
  6. function prepareURLListForDiagnostics(urls) {
  7. const urlList = urls || []; // Initialize with an empty array if input is null or undefined
  8. const errorMessages = {};
  9. for (const url of urlList) {
  10. try {
  11. // Basic URL validation (you can add more sophisticated checks)
  12. if (typeof url !== 'string' || url.trim() === '') {
  13. errorMessages[url] = "Invalid URL format.";
  14. continue; // Skip to the next URL
  15. }
  16. new URL(url); // Attempt to create a URL object - throws error if invalid
  17. } catch (error) {
  18. errorMessages[url] = `Error: ${error.message}`; // Capture and store the error message
  19. }
  20. }
  21. return { urlList, errorMessages };
  22. }
  23. // Example usage (for testing)
  24. // const urls = ["https://www.google.com", "invalid-url", "https://example.com"];
  25. // const diagnostics = prepareURLListForDiagnostics(urls);
  26. // console.log(diagnostics);

Add your comment