Architectural Overview¶
Error Translator is built around one deterministic engine in error_translator/core.py. The same engine powers the CLI, auto-hook mode, Python imports, and the FastAPI service.
Core Translation Pipeline¶
Each translation request follows this sequence:
- Load and cache rule data from
src/error_translator/rules.json. - Extract traceback location (
file,line) when present. - Read source context line using
linecachewhen file and line are available. - Match the last error line against pre-compiled regex patterns.
- First attempt uses the optional C extension matcher (
error_translator.fast_matcher.match_loop). - If the extension is unavailable, the engine falls back to the pure Python loop.
- First attempt uses the optional C extension matcher (
- Format output fields (
explanation,fix, and metadata). - Optionally attach
ast_insightfor registered error types.
If no pattern matches, the default rule is returned.
Implementation Surfaces¶
The same engine is exposed through:
- CLI (
error_translator/cli.py):explain-error run script.pyexplain-error "<error text>"cat error.log | explain-error
- Auto hook (
error_translator/auto.py): import once to overridesys.excepthook. - Python API:
from error_translator.core import translate_error. - FastAPI service (
error_translator/api/server.py):POST /translateGET /healthGET /serves the bundled static UI (if present)
AST Insight Notes¶
ast_insight is optional and routed via AST_REGISTRY in error_translator/ast_handlers.py.
Current handlers are lightweight placeholders and return suggestion strings for supported error classes (NameError, AttributeError, ImportError). They do not yet perform full file AST traversal.
Runtime Contract¶
translate_error(...) always returns a dictionary with these fields:
explanationfix
And typically includes:
matched_errorfile(orUnknown File)line(orUnknown Line)code(empty string if unavailable)ast_insight(only when available)
Extension Strategy¶
For most contributions, follow this order:
- Add or improve a regex rule in
src/error_translator/rules.json. - Add tests in
tests/test_core.py. - Add/update handler behavior in
error_translator/ast_handlers.pyonly when regex alone is insufficient. - Update
error_translator/core.pyonly for shared pipeline behavior.
Optional Native Acceleration¶
The optional C extension source lives at src/error_translator/ext/fast_matcher.c and is built via setup_ext.py.
When built successfully, core translation uses native code for the rule scan loop. When not built, behavior remains identical because the Python loop is used automatically.