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