Reviewer · Error Handling
Don't mask original errors
Secondary logic that runs alongside or after a primary operation — usage/telemetry accounting in a `finally` block, optional hooks, cleanup code — must never be allowed to raise and replace the real exception from the operation it is wrapping. If such code can fail (e.g. a property access that touches sub-objects, a user-supplied callback), wrap it in its...
Secondary logic that runs alongside or after a primary operation — usage/telemetry accounting in a finally block, optional hooks, cleanup code — must never be allowed to raise and replace the real exception from the operation it is wrapping. If such code can fail (e.g. a property access that touches sub-objects, a user-supplied callback), wrap it in its own try/except Exception: pass (or log-and-continue) so a failure there is swallowed rather than overwriting the original traceback:
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 result
pass
Conversely, do not add blanket try/except around code paths where a failure indicates a genuine bug rather than an expected, recoverable condition (for example, swallowing import/export errors in an __init__.py) — let those exceptions propagate so the underlying defect is visible and fixable, instead of hiding it behind a silent fallback.
When you do need to catch and re-raise (e.g. to add context to a nested serialization or validation failure), use raise NewError(...) from original_error rather than discarding the original exception. This keeps the full cause chain available in tracebacks and logs, so a failure several levels deep (e.g. Outer.inner (Inner) is not serializable caused by Inner.lock (lock) is not serializable) can still be traced back to its root cause without extra work from the caller. Apply this consistently: only catch exceptions you have a specific plan for, and always preserve enough information (via chaining or explicit messages) for the original failure to be diagnosable.</description>
</invoke>