Skip to main content

capture_error

Function capture_error 

Source
pub fn capture_error<E: Error + ?Sized + 'static>(error: &E)
Expand description

Records error as the error of the tool call that is running, for AgentCat to report with the call: its Display text, its type path (std::any::type_name), the text of each error of its source() chain (at most 10), and a backtrace of this call site when the environment turns backtraces on (RUST_BACKTRACE or RUST_LIB_BACKTRACE, see Backtrace::capture). The backtrace is turned into text later, off the tool’s task.

rmcp turns the error a tool returns into the text of an is_error result before AgentCat sees it, so the type and the chain are lost. Call this before the tool returns the error: when the call then fails (an is_error result or an Err(ErrorData)), the recorded error takes the place of the one AgentCat reads from the result. When the call succeeds, nothing is reported. When the tool panics, the panic is reported instead. When a tool calls this function more than once, the last error counts. The tool’s result and error reach the client unchanged.

error may be a dyn Error, for example &*boxed. Its recorded type is then the name of the dyn type (dyn core::error::Error), not that of the value behind it.

The call is found through a tokio task-local value that AgentCat sets while it runs the tool’s future. Outside a tool call that AgentCat traces, this function does nothing. A task the tool spawns (tokio::spawn) or a thread it starts does not belong to the call, so capture_error does nothing there either: call it from the tool’s own future.

use agentcat_sdk::capture_error;

fn read_config(path: &str) -> Result<String, String> {
    std::fs::read_to_string(path).map_err(|error| {
        capture_error(&error);
        error.to_string()
    })
}