Tighten exception handling in helpers.py

- Remove silent except around content serialization (was hiding bugs)
- Narrow cookie/request-context catches to RuntimeError
- Narrow script hash to OSError
- Log warnings for pretty-print failures instead of silent pass

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-06 21:19:26 +00:00
parent 2660d37f9e
commit 227444a026

View File

@@ -657,10 +657,7 @@ def _build_pages_sx(service: str) -> str:
for page_def in pages.values():
content_src = ""
if page_def.content_expr is not None:
try:
content_src = sx_serialize(page_def.content_expr)
except Exception:
pass
content_src = sx_serialize(page_def.content_expr)
auth = page_def.auth if isinstance(page_def.auth, str) else "custom"
has_data = "true" if page_def.data_expr is not None else "false"
@@ -742,8 +739,9 @@ def sx_page(ctx: dict, page_sx: str, *,
from .parser import parse as _parse, serialize as _serialize
try:
page_sx = _serialize(_parse(page_sx), pretty=True)
except Exception:
pass
except Exception as e:
import logging
logging.getLogger("sx").warning("Pretty-print page_sx failed: %s", e)
# Style dictionary for client-side css primitive
styles_hash = _get_style_dict_hash()
@@ -826,7 +824,7 @@ def _get_sx_styles_cookie() -> str:
try:
from quart import request
return request.cookies.get("sx-styles-hash", "")
except Exception:
except RuntimeError:
return ""
@@ -836,7 +834,7 @@ def _script_hash(filename: str) -> str:
try:
data = (Path("static") / "scripts" / filename).read_bytes()
_SCRIPT_HASH_CACHE[filename] = hashlib.md5(data).hexdigest()[:8]
except Exception:
except OSError:
_SCRIPT_HASH_CACHE[filename] = "dev"
return _SCRIPT_HASH_CACHE[filename]
@@ -846,7 +844,7 @@ def _get_csrf_token() -> str:
try:
from quart import g
return getattr(g, "csrf_token", "")
except Exception:
except RuntimeError:
return ""
@@ -855,7 +853,7 @@ def _get_sx_comp_cookie() -> str:
try:
from quart import request
return request.cookies.get("sx-comp-hash", "")
except Exception:
except RuntimeError:
return ""
@@ -899,7 +897,9 @@ def _pretty_print_sx_body(body: str) -> str:
pretty_parts = [_serialize(expr, pretty=True) for expr in exprs]
parts.append("\n\n".join(pretty_parts))
return "\n\n".join(parts)
except Exception:
except Exception as e:
import logging
logging.getLogger("sx").warning("Pretty-print sx body failed: %s", e)
return body