Private
Public Access
TIER-2 READ conductor/code_styleguides/error_handling.md end-to-end before Phase 5: refactor(gui_2): migrate L3582 render_context_batch_actions preview to Result[T] (Phase 5)
Extract _render_context_batch_actions_preview_result helper from the _do_generate preview try/except in render_context_batch_actions. The imgui.button callback drains errors to app._last_request_errors per FR-BC-4 event-handler pattern. [pre-audit] L3582 INTERNAL_BROAD_CATCH [post-audit] V count: 8 -> 7 (L3582 removed)
This commit is contained in:
+36
-7
@@ -3535,13 +3535,11 @@ def render_context_batch_actions(app: App, total_lines: int, total_ast: int) ->
|
|||||||
if not app.context_files:
|
if not app.context_files:
|
||||||
app.context_preview_text = "# Context Composition Empty\n\nNo files have been added to the context composition yet."
|
app.context_preview_text = "# Context Composition Empty\n\nNo files have been added to the context composition yet."
|
||||||
else:
|
else:
|
||||||
try:
|
preview_result = _render_context_batch_actions_preview_result(app)
|
||||||
app.controller.context_files = app.context_files
|
app.context_preview_text = preview_result.data
|
||||||
app.context_preview_text = app.controller._do_generate()[0]
|
if not preview_result.ok:
|
||||||
except Exception as e:
|
if not hasattr(app, '_last_request_errors'): app._last_request_errors = []
|
||||||
import traceback
|
app._last_request_errors.append(("_render_context_batch_actions_preview", preview_result.errors[0]))
|
||||||
err = traceback.format_exc()
|
|
||||||
app.context_preview_text = f"# Error generating preview\n\n```python\n{err}\n```"
|
|
||||||
app.show_windows["Context Preview"] = True
|
app.show_windows["Context Preview"] = True
|
||||||
imgui.same_line()
|
imgui.same_line()
|
||||||
imgui.text(f" | Total: {len(app.context_files)} files, {total_lines} lines, {total_ast} AST elements")
|
imgui.text(f" | Total: {len(app.context_files)} files, {total_lines} lines, {total_ast} AST elements")
|
||||||
@@ -7931,6 +7929,37 @@ def _render_tool_preset_bias_save_result(app: "App") -> Result[bool]:
|
|||||||
original=e,
|
original=e,
|
||||||
)])
|
)])
|
||||||
|
|
||||||
|
|
||||||
|
def _render_context_batch_actions_preview_result(app: "App") -> Result[str]:
|
||||||
|
"""Drain-aware variant of L3582 render_context_batch_actions Preview button.
|
||||||
|
|
||||||
|
Extracts the _do_generate preview try/except from
|
||||||
|
render_context_batch_actions into a Result-returning helper. On success,
|
||||||
|
returns Result(data=preview_text) where preview_text is the
|
||||||
|
controller._do_generate() output. On exception, captures the traceback,
|
||||||
|
returns Result(data="<error markdown>", errors=[ErrorInfo]).
|
||||||
|
|
||||||
|
The legacy wrapper (the imgui.button callback) drains errors to
|
||||||
|
app._last_request_errors (per FR-BC-4 event-handler drain pattern;
|
||||||
|
data plane attribute).
|
||||||
|
|
||||||
|
[C: src/gui_2.py:render_context_batch_actions (L3582 legacy wrapper)]
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
app.controller.context_files = app.context_files
|
||||||
|
preview_text = app.controller._do_generate()[0]
|
||||||
|
return Result(data=preview_text)
|
||||||
|
except Exception as e:
|
||||||
|
import traceback
|
||||||
|
err = traceback.format_exc()
|
||||||
|
preview_text = f"# Error generating preview\n\n```python\n{err}\n```"
|
||||||
|
return Result(data=preview_text, errors=[ErrorInfo(
|
||||||
|
kind=ErrorKind.INTERNAL,
|
||||||
|
message=f"Context preview generation failed: {e}",
|
||||||
|
source="gui_2._render_context_batch_actions_preview_result",
|
||||||
|
original=e,
|
||||||
|
)])
|
||||||
|
|
||||||
#endregion: Phase 5 Event Handler Result Helpers
|
#endregion: Phase 5 Event Handler Result Helpers
|
||||||
|
|
||||||
#endregion: MMA
|
#endregion: MMA
|
||||||
|
|||||||
@@ -1068,3 +1068,42 @@ def test_phase_5_l3163_render_tool_preset_bias_save_result_failure():
|
|||||||
assert err.source == "gui_2._render_tool_preset_bias_save_result"
|
assert err.source == "gui_2._render_tool_preset_bias_save_result"
|
||||||
assert "bias validation failed" in err.message
|
assert "bias validation failed" in err.message
|
||||||
assert "Error:" in app.ai_status
|
assert "Error:" in app.ai_status
|
||||||
|
|
||||||
|
|
||||||
|
def test_phase_5_l3582_render_context_batch_actions_preview_result_success():
|
||||||
|
"""
|
||||||
|
L3582 _render_context_batch_actions_preview_result returns Result.ok=True on success.
|
||||||
|
|
||||||
|
The helper wraps the _do_generate preview try/except in
|
||||||
|
render_context_batch_actions. On success, returns Result(data=preview_text)
|
||||||
|
where preview_text is the controller._do_generate() output.
|
||||||
|
"""
|
||||||
|
from src import gui_2
|
||||||
|
from unittest.mock import MagicMock
|
||||||
|
app = MagicMock()
|
||||||
|
app.context_files = ["foo.py", "bar.py"]
|
||||||
|
app.controller._do_generate.return_value = ("# Generated Preview\n\nContent here", "preview.md")
|
||||||
|
result = gui_2._render_context_batch_actions_preview_result(app)
|
||||||
|
assert result.ok, f"Expected ok=True on success, got errors: {result.errors}"
|
||||||
|
assert "Generated Preview" in result.data
|
||||||
|
|
||||||
|
|
||||||
|
def test_phase_5_l3582_render_context_batch_actions_preview_result_failure():
|
||||||
|
"""
|
||||||
|
L3582 _render_context_batch_actions_preview_result returns Result.ok=False on failure.
|
||||||
|
|
||||||
|
When _do_generate raises, the helper captures the traceback and returns
|
||||||
|
Result(data="<error message>", errors=[ErrorInfo]).
|
||||||
|
"""
|
||||||
|
from src import gui_2
|
||||||
|
from unittest.mock import MagicMock
|
||||||
|
app = MagicMock()
|
||||||
|
app.context_files = ["foo.py"]
|
||||||
|
app.controller._do_generate.side_effect = RuntimeError("generate failed")
|
||||||
|
result = gui_2._render_context_batch_actions_preview_result(app)
|
||||||
|
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_context_batch_actions_preview_result"
|
||||||
|
assert "generate failed" in err.message
|
||||||
|
assert "Error" in result.data
|
||||||
|
|||||||
Reference in New Issue
Block a user