mass_queue icon indicating copy to clipboard operation
mass_queue copied to clipboard

[Bug] Unhandled InvalidUrlClientError in utils.py (HA OS 17 RC) - Fix Proposal Included (only for beta tester 🚀)

Open Asmir1975 opened this issue 11 months ago • 0 comments

Describe the bug I encountered a recurring InvalidUrlClientError in custom_components/mass_queue/utils.py while running Home Assistant OS 17 (RC). The error occurs when the download_and_encode_image function receives an empty or invalid URL, causing a traceback and potentially freezing the queue image update in the frontend.

error.log

Analysis The function download_and_encode_image does not validate the url input before passing it to session.get(). If url is None or an empty string, aiohttp raises an exception that is not caught.

Proposed Fix I successfully tested the following modification locally. It adds input validation and a try/except block to handle connection errors gracefully (without crashing).

In utils.py:

Click to view python Prototype
async def download_and_encode_image(url: str, hass: HomeAssistant):
    """Downloads and encodes a single image from the given URL."""
    # --- FIX START: Validate Input ---
    if not url or not isinstance(url, str):
        # Optional: LOGGER.debug("download_and_encode_image received invalid URL") 
        return None
    # --- FIX END ---
    
    session = aiohttp_client.async_get_clientsession(hass)
    
    # --- FIX START: Handle Connection Errors ---
    try:
        req = await session.get(url)
        # Ensure we only process valid responses (200 OK)
        if req.status != 200:
             return None
             
        read = await req.content.read()
        return f"data:image;base64,{base64.b64encode(read).decode('utf-8')}"
    except Exception:
        # Gracefully handle network errors or invalid URLs
        return None
    # --- FIX END ---

Additional Context

Environment: Home Assistant OS 17 (RC/Beta)

Component: mass_queue

Asmir1975 avatar Dec 16 '25 08:12 Asmir1975