IntoToolError

Derive Macro IntoToolError 

Source
#[derive(IntoToolError)]
{
    // Attributes available to this derive:
    #[tool_error]
}
Expand description

Derives automatic conversion from an error enum to ToolError.

This macro generates a From<YourError> for ToolError implementation that automatically classifies errors as retriable or permanent based on naming conventions in variant names.

§Classification Rules

Errors are classified as retriable if their variant names contain:

  • Rpc, Network, Connection, Timeout, TooManyRequests, RateLimit
  • Api (for external API errors)
  • Http (for HTTP-related errors)

Errors are classified as permanent if their variant names contain:

  • Invalid, Parse, Serialization, NotFound, Unauthorized
  • InsufficientBalance, InsufficientFunds
  • All other unmatched variants (conservative default)

§Best Practices

This derive macro is the recommended way to handle custom errors for tools. It provides:

  • Automatic error classification based on variant names
  • Override capabilities for fine-grained control
  • Type-safe error handling
  • Consistent error conversion across your codebase

Using this macro instead of string-based error handling ensures that your errors are properly structured and can be downcast by upstream consumers for specific error handling logic.

§Custom Classification

You can override the automatic classification using attributes:

#[derive(IntoToolError)]
enum MyError {
    #[tool_error(retriable)]
    CustomError(String),

    #[tool_error(permanent)]
    NetworkError(String), // Override default retriable classification

    #[tool_error(rate_limited)]
    ApiQuotaExceeded,
}

§Examples

use riglr_macros::IntoToolError;
use thiserror::Error;

#[derive(Error, Debug, IntoToolError)]
enum SolanaError {
    #[error("RPC error: {0}")]
    RpcError(String),  // Automatically retriable

    #[error("Invalid address: {0}")]
    InvalidAddress(String),  // Automatically permanent

    #[error("Network timeout")]
    NetworkTimeout,  // Automatically retriable

    #[error("Insufficient balance")]
    InsufficientBalance,  // Automatically permanent

    #[tool_error(retriable)]
    #[error("Custom error: {0}")]
    CustomError(String),  // Explicitly retriable
}