Files
MajoorWaldi-ComfyUI-Majoor-…/mjr_am_shared/errors.py
T
MajoorWaldi 08e07b5312 fix: replace silent catches with debug logging across multiple files
- Updated error handling in videoProcessor.js, videoProcessorWebGL.js, videoSync.js, viewerInstanceManager.js, and various utility files to log caught exceptions using console.debug.
- Introduced a new script to automate the replacement of silent catch blocks with logging statements.
- Enhanced safety checks in the database schema and transaction manager to prevent SQL injection vulnerabilities.
- Added regex patterns for validating column definitions and executable names in the ExifTool adapter.
- Improved error handling in health and filesystem routes to ensure better feedback on invalid inputs.
- Added tests to cover new error handling and validation logic.
2026-02-27 21:00:17 +01:00

62 lines
1.6 KiB
Python

"""
Shared helpers for sanitizing error messages before they reach clients.
"""
from __future__ import annotations
import os
import re
from typing import Any
from .log import get_logger
logger = get_logger(__name__)
_DEBUG_MODE = os.getenv("MJR_DEBUG", "").strip().lower() in ("1", "true", "yes", "on")
_WINDOWS_PATH_RE = re.compile(r"[A-Za-z]:\\[^\s]+")
_UNC_PATH_RE = re.compile(r"\\\\[^\s\\]+\\[^\s]+")
_UNIX_PATH_RE = re.compile(r"(?<![A-Za-z0-9:/?&=#%])/(?!/)[^\s#?]+")
def _mask_paths(value: str) -> str:
"""Mask path-looking substrings to avoid leaking filesystem structure."""
cleaned = _WINDOWS_PATH_RE.sub("[path]", value)
cleaned = _UNC_PATH_RE.sub("[path]", cleaned)
cleaned = _UNIX_PATH_RE.sub("[path]", cleaned)
return cleaned
def sanitize_error_message(exc: Any, fallback: str) -> str:
"""
Build a safe error message for clients.
Args:
exc: Exception or raw value to sanitize.
fallback: Fallback message to show when nothing meaningful remains.
Returns:
A string suitable for inclusion in API responses.
"""
if not fallback:
fallback = "An error occurred"
if exc is None:
return fallback
try:
raw = str(exc)
except Exception:
raw = ""
if not raw:
return fallback
expanded = os.path.expandvars(raw)
sanitized = _mask_paths(expanded.replace(os.getcwd(), "[cwd]"))
sanitized = " ".join(sanitized.splitlines()).strip()
if _DEBUG_MODE:
logger.debug("Sanitized error payload: %s", sanitized, exc_info=True)
if sanitized:
return f"{fallback}: {sanitized[:200]}"
return fallback