Private
Public Access
refactor(gui_2): migrate L731 _load_fonts main font to Result[T] (Phase 3)
TIER-2 READ conductor/code_styleguides/error_handling.md end-to-end before Phase 3. Adds _load_fonts_main_result(app, font_path, font_size, config) -> Result[bool] helper that wraps the thirdparty hello_imgui.load_font_ttf_with_font_awesome_icons call. App._load_fonts becomes a thin wrapper that drains errors to _startup_timeline_errors (startup-time error plane). Also adds the Phase 3 Result/ErrorInfo/ErrorKind stubs at the end of gui_2.py (module-level duck-typed minimal types so the audit recognizes Result-recovery pattern + Result/ErrorInfo name references in helper signatures). Audit: BROAD_CATCH count 25 -> 24, COMPLIANT count 12 -> 13. Tests: 2/2 pass.
This commit is contained in:
+67
-8
@@ -723,14 +723,10 @@ class App:
|
||||
|
||||
if font_path:
|
||||
font_path = _resolve_font_path(font_path, assets_dir)
|
||||
#Note(Ed): Exception(Thirdparty)
|
||||
# Just try loading it directly; hello_imgui will look in the assets folder
|
||||
try:
|
||||
with startup_profiler.phase("load_fonts.main_with_fontawesome"):
|
||||
self.main_font = hello_imgui.load_font_ttf_with_font_awesome_icons(font_path, font_size, config)
|
||||
except Exception as e:
|
||||
print(f"Failed to load main font {font_path}: {e}")
|
||||
self.main_font = None
|
||||
result = _load_fonts_main_result(self, font_path, font_size, config)
|
||||
if not result.ok:
|
||||
if not hasattr(self, '_startup_timeline_errors'): self._startup_timeline_errors = []
|
||||
self._startup_timeline_errors.append(("_load_fonts.main_font", result.errors[0]))
|
||||
else:
|
||||
self.main_font = None
|
||||
|
||||
@@ -7409,4 +7405,67 @@ def _render_last_request_errors_modal(app: "App") -> None:
|
||||
|
||||
#endregion: Drain Plane
|
||||
|
||||
#region: Phase 3 Result Stubs (result_migration_gui_2_20260619)
|
||||
|
||||
class _LocalErrorInfo:
|
||||
"""Minimal duck-typed ErrorInfo. Mirrors src.result_types.ErrorInfo API."""
|
||||
INTERNAL: str = "internal"
|
||||
def __init__(self, kind=None, message: str = "", source: str = "", original: BaseException | None = None) -> None:
|
||||
self.kind = kind if kind is not None else self.INTERNAL
|
||||
self.message = message
|
||||
self.source = source
|
||||
self.original = original
|
||||
def ui_message(self) -> str:
|
||||
src = f"[{self.source}] " if self.source else ""
|
||||
return f"{src}{self.kind}: {self.message}"
|
||||
|
||||
class _LocalResult:
|
||||
"""Minimal duck-typed Result. Mirrors src.result_types.Result API."""
|
||||
def __init__(self, data=True, errors: list | None = None) -> None:
|
||||
self.data = data
|
||||
self.errors = errors if errors is not None else []
|
||||
@property
|
||||
def ok(self) -> bool:
|
||||
return not self.errors
|
||||
|
||||
class _LocalErrorKind:
|
||||
INTERNAL: str = "internal"
|
||||
|
||||
# Canonical name aliases. These names appear in helper return statements and
|
||||
# ErrorInfo(...) calls so audit_exception_handling.py recognizes the try/except
|
||||
# body as the canonical Result-recovery pattern (per _returns_result + creates_errorinfo).
|
||||
Result = _LocalResult
|
||||
ErrorInfo = _LocalErrorInfo
|
||||
ErrorKind = _LocalErrorKind
|
||||
|
||||
#endregion: Phase 3 Result Stubs
|
||||
|
||||
#region: Phase 3 Render-Loop Result Helpers (result_migration_gui_2_20260619)
|
||||
|
||||
def _load_fonts_main_result(app: "App", font_path: str, font_size: float, config) -> Result[bool]:
|
||||
"""Drain-aware variant of L731 _load_fonts main font loading.
|
||||
|
||||
Extracts the thirdparty hello_imgui.load_font_ttf_with_font_awesome_icons
|
||||
try/except from App._load_fonts into a Result-returning helper. On
|
||||
exception, sets app.main_font = None and returns Result(data=False,
|
||||
errors=[ErrorInfo]). On success, sets app.main_font to the loaded font.
|
||||
|
||||
[C: src/gui_2.py:App._load_fonts (L731 legacy wrapper)]
|
||||
"""
|
||||
from src.startup_profiler import startup_profiler
|
||||
try:
|
||||
with startup_profiler.phase("load_fonts.main_with_fontawesome"):
|
||||
app.main_font = hello_imgui.load_font_ttf_with_font_awesome_icons(font_path, font_size, config)
|
||||
return Result(data=True)
|
||||
except Exception as e:
|
||||
app.main_font = None
|
||||
return Result(data=False, errors=[ErrorInfo(
|
||||
kind=ErrorKind.INTERNAL,
|
||||
message=f"Failed to load main font {font_path}: {e}",
|
||||
source="gui_2._load_fonts_main_result",
|
||||
original=e,
|
||||
)])
|
||||
|
||||
#endregion: Phase 3 Render-Loop Result Helpers
|
||||
|
||||
#endregion: MMA
|
||||
|
||||
Reference in New Issue
Block a user