TIER-2 READ conductor/code_styleguides/error_handling.md end-to-end before Phase 4: refactor(gui_2): migrate L3718 render_ast_inspector_modal outline to Result[T] (Phase 4)

Adds _render_ast_inspector_outline_result(app, f_path) -> Result[str] helper that wraps
the mcp_client.configure + outline fetch try/except in render_ast_inspector_modal.
The data field carries the outline string so the legacy wrapper can iterate it
without an additional instance attribute. Errors drain to app._last_request_errors
(per FR-BC-3 modal pattern; data plane attribute).

Audit: BROAD_CATCH count 16 -> 15, COMPLIANT count 21 -> 22. Migration
target count drops by 1. Tests: 2/2 pass.
This commit is contained in:
ed
2026-06-19 22:48:43 -04:00
parent 1ef0e07093
commit e558da81e1
2 changed files with 79 additions and 10 deletions
+39 -10
View File
@@ -3707,16 +3707,11 @@ def render_ast_inspector_modal(app: App) -> None:
f_path = f_item.path if hasattr(f_item, "path") else str(f_item)
if f_path != getattr(app, '_cached_ast_file_path', None):
outline = ""
try:
proj_dir = str(Path(app.controller.active_project_path).parent.resolve()) if getattr(app, 'controller', None) and app.controller.active_project_path else None
mcp_client.configure([{"path": f_path}], [proj_dir] if proj_dir else None)
if f_path.lower().endswith('.py'): outline = mcp_client.py_get_code_outline(f_path)
elif f_path.lower().endswith(('.c', '.h')): outline = mcp_client.ts_c_get_code_outline(f_path)
elif f_path.lower().endswith(('.cpp', '.hpp', '.cxx', '.cc')): outline = mcp_client.ts_cpp_get_code_outline(f_path)
except Exception as e:
outline = f"Error fetching outline: {e}"
outline_result = _render_ast_inspector_outline_result(app, f_path)
if not outline_result.ok:
if not hasattr(app, '_last_request_errors'): app._last_request_errors = []
app._last_request_errors.append(("render_ast_inspector.outline", outline_result.errors[0]))
outline = outline_result.data
app._cached_ast_nodes = []
import re #TODO(Ed): Review local import
@@ -7670,9 +7665,43 @@ def _render_persona_editor_save_result(app: "App") -> Result[bool]:
kind=ErrorKind.INTERNAL,
message=f"Persona save failed: {e}",
source="gui_2._render_persona_editor_save_result",
)])
def _render_ast_inspector_outline_result(app: "App", f_path: str) -> Result[str]:
"""Drain-aware variant of L3718 render_ast_inspector_modal outline fetch try/except.
Extracts the mcp_client.configure + mcp_client.{py,ts_c,ts_cpp}_get_code_outline
try/except from render_ast_inspector_modal into a Result-returning helper.
On success, returns Result(data=outline) where outline is the string from
the appropriate outline function (or "" if no extension matched). On
failure (any exception during configure or outline fetch), returns
Result(data=f"Error fetching outline: {e}", errors=[ErrorInfo]) so the
legacy caller can still display a meaningful error message.
The data field carries the outline string so the legacy wrapper can
iterate it without an additional instance attribute. Errors drain to
app._last_request_errors (per FR-BC-3 modal pattern; data plane).
[C: src/gui_2.py:render_ast_inspector_modal (L3718 legacy wrapper)]
"""
try:
proj_dir = str(Path(app.controller.active_project_path).parent.resolve()) if getattr(app, 'controller', None) and app.controller.active_project_path else None
mcp_client.configure([{"path": f_path}], [proj_dir] if proj_dir else None)
outline = ""
if f_path.lower().endswith('.py'): outline = mcp_client.py_get_code_outline(f_path)
elif f_path.lower().endswith(('.c', '.h')): outline = mcp_client.ts_c_get_code_outline(f_path)
elif f_path.lower().endswith(('.cpp', '.hpp', '.cxx', '.cc')): outline = mcp_client.ts_cpp_get_code_outline(f_path)
return Result(data=outline)
except Exception as e:
return Result(data=f"Error fetching outline: {e}", errors=[ErrorInfo(
kind=ErrorKind.INTERNAL,
message=f"AST outline fetch failed for {f_path}: {e}",
source="gui_2._render_ast_inspector_outline_result",
original=e,
)])
#endregion: Phase 4 Modal/Dialog Result Helpers
#endregion: Phase 4 Modal/Dialog Result Helpers
#endregion: MMA
+40
View File
@@ -658,3 +658,43 @@ def test_phase_4_l3398_render_persona_editor_save_result_failure():
assert err.source == "gui_2._render_persona_editor_save_result"
assert "validation failed" in err.message
assert "Error:" in app.ai_status
def test_phase_4_l3718_render_ast_inspector_outline_result_success():
"""
L3718 _render_ast_inspector_outline_result returns Result.ok=True on success.
The helper wraps the mcp_client.{py,ts_c,ts_cpp}_get_code_outline try/except
in render_ast_inspector_modal. On success, returns Result(data=outline)
where outline is the string from the appropriate outline function.
"""
from src import gui_2
from unittest.mock import MagicMock, patch
app = MagicMock()
app.controller.active_project_path = "/proj/foo"
with patch.object(gui_2.mcp_client, "configure") as _cfg, \
patch.object(gui_2.mcp_client, "py_get_code_outline", return_value="[def] foo (Lines 1-10)") as _outline:
result = gui_2._render_ast_inspector_outline_result(app, "/proj/foo/src/bar.py")
assert result.ok, f"Expected ok=True on success, got errors: {result.errors}"
assert result.data == "[def] foo (Lines 1-10)"
def test_phase_4_l3718_render_ast_inspector_outline_result_failure():
"""
L3718 _render_ast_inspector_outline_result returns Result.ok=False on failure.
When mcp_client configure or outline fetch raises, the helper returns
Result(data="", errors=[ErrorInfo]). The legacy wrapper should drain
to app._last_request_errors (per FR-BC-3 modal pattern).
"""
from src import gui_2
from unittest.mock import MagicMock, patch
app = MagicMock()
app.controller.active_project_path = "/proj/foo"
with patch.object(gui_2.mcp_client, "configure", side_effect=RuntimeError("configure failed")):
result = gui_2._render_ast_inspector_outline_result(app, "/proj/foo/src/bar.py")
assert not result.ok, f"Expected ok=False on failure, got data: {result.data}"
assert result.errors, "Expected at least one error on failure"
err = result.errors[0]
assert err.source == "gui_2._render_ast_inspector_outline_result"
assert "Error fetching outline" in result.data