Reviewer · Error Handling
Preserve original exception context
Never let secondary error-handling code hide or replace the root cause of a failure. 1. Code that runs alongside or after a primary operation — usage/telemetry accounting, logging hooks, bookkeeping — must not be allowed to raise from a `finally` block and overwrite the real exception. Wrap such side effects in their own narrow try/except so a failure...
Never let secondary error-handling code hide or replace the root cause of a failure.
- Code that runs alongside or after a primary operation — usage/telemetry accounting, logging hooks, bookkeeping — must not be allowed to raise from a
finallyblock and overwrite the real exception. Wrap such side effects in their own narrow try/except so a failure there is swallowed (or logged) rather than propagated:
try:
final_answer = agent.run(task)
finally:
if usage_hook is not None:
try:
usage_hook(agent.usage)
except Exception: # accounting must never mask the run's real error
pass
Without this guard, a broken usage property or hook turns a meaningful AgentRunError into an unrelated AttributeError, and the caller loses the actual failure.
- When catching an exception from a nested/inner call and re-raising it as a higher-level, more descriptive error, always chain it with
raise NewError(...) from original_errorinstead of swallowing or flattening it into a bare string. This keeps every level of the failure visible in the traceback (innermost cause first), which matters most in multi-level wrapping (e.g., nested serialization, nested tool calls) where the top-level message alone does not name the actual offending attribute or operation:
try:
return attr_value.to_dict(strict)
except (TypeError, ValueError) as error:
raise TypeError(
f"{owner}.{attr_name} ({type_name}) is not serializable"
) from error
Apply both rules consistently: isolate non-essential error-handling side effects so they cannot mask the primary failure, and always preserve the exception chain when wrapping errors so the root cause remains discoverable.