Improve error handling to help prevent runtime errors

Adds input validation and safe property access to utility functions
to handle edge cases and invalid arguments gracefully
This commit is contained in:
Eddy G
2025-07-07 12:26:59 -04:00
parent a3c581aa93
commit 9c5ed0dcca
3 changed files with 18 additions and 4 deletions

View File

@@ -297,7 +297,7 @@ class AutoComplete {
// if a click is detected on the page, generally we hide the suggestions, unless the click was within the autocomplete elements // if a click is detected on the page, generally we hide the suggestions, unless the click was within the autocomplete elements
checkOutsideClick(e) { checkOutsideClick(e) {
if (e.target.id === 'txtLocation') return; if (e.target.id === 'txtLocation') return;
if (e.target?.parentNode?.classList.contains(this.options.containerClass)) return; if (e.target?.parentNode?.classList?.contains(this.options.containerClass)) return;
this.hideSuggestions(); this.hideSuggestions();
} }
} }

View File

@@ -5,6 +5,11 @@ import { blob } from './fetch.mjs';
// a list of cached icons is used to avoid hitting the cache multiple times // a list of cached icons is used to avoid hitting the cache multiple times
const cachedImages = []; const cachedImages = [];
const preloadImg = (src) => { const preloadImg = (src) => {
if (!src || typeof src !== 'string') {
console.warn(`preloadImg expects a URL string, received: '${src}' (${typeof src})`);
return false;
}
if (cachedImages.includes(src)) return false; if (cachedImages.includes(src)) return false;
blob(src); blob(src);
cachedImages.push(src); cachedImages.push(src);

View File

@@ -1,14 +1,23 @@
// rewrite URLs to use local proxy server // rewrite URLs to use local proxy server
const rewriteUrl = (_url) => { const rewriteUrl = (_url) => {
// Handle relative URLs - return them as-is since they don't need rewriting if (!_url) {
throw new Error(`rewriteUrl called with invalid argument: '${_url}' (${typeof _url})`);
}
// Handle relative URLs early: return them as-is since they don't need rewriting
if (typeof _url === 'string' && !_url.startsWith('http')) { if (typeof _url === 'string' && !_url.startsWith('http')) {
return _url; return _url;
} }
// Handle both string URLs and URL objects if (typeof _url !== 'string' && !(_url instanceof URL)) {
const url = typeof _url === 'string' ? new URL(_url) : new URL(_url.toString()); throw new Error(`rewriteUrl expects a URL string or URL object, received: ${typeof _url}`);
}
// Convert to URL object (for URL objects, creates a copy to avoid mutating the original)
const url = new URL(_url);
if (!window.WS4KP_SERVER_AVAILABLE) { if (!window.WS4KP_SERVER_AVAILABLE) {
// If running standalone in the browser, simply return a URL object without rewriting
return url; return url;
} }