agent: Improve error handling.