Module

compiler.statements.error_handling

Error boundary compilation for Kida compiler.

Compiles {% try %}...{% fallback %}...{% end %} to Python try/except with sub-buffer management for streaming safety.

Uses inline TYPE_CHECKING declarations for host attributes. See: plan/rfc-mixin-protocol-typing.md

Classes

ErrorHandlingMixin 1
Mixin for compiling error boundary statements. Host attributes and cross-mixin dependencies are de…

Mixin for compiling error boundary statements.

Host attributes and cross-mixin dependencies are declared via inline TYPE_CHECKING blocks.

Methods

Internal Methods 1
_compile_try 1 list[ast.stmt]
Compile {% try %}...{% fallback %}...{% end %} error boundary. Both StringBuil…
def _compile_try(self, node: Try) -> list[ast.stmt]

Compile {% try %}...{% fallback %}...{% end %} error boundary.

Both StringBuilder and streaming modes use the same sub-buffer strategy for the try body (must buffer to discard on error).

Generated code (StringBuilder mode):

_try_buf_N = []
_try_append_N = _try_buf_N.append
_saved_append_N = _append
_append = _try_append_N
try:
    # body compiled with _append → sub-buffer
    _append = _saved_append_N
    _append(''.join(_try_buf_N))
except (...) as _err_N:
    _append = _saved_append_N
    # fallback body

Generated code (streaming mode):

_try_buf_N = []
_append = _try_buf_N.append
try:
    # body compiled in non-streaming mode → _append to buffer
    for _chunk in _try_buf_N: yield _chunk
except (...) as _err_N:
    # fallback body compiled in streaming mode (yields)
Parameters
Name Type Description
node
Returns
list[ast.stmt]