ExtensionError

Trait ExtensionError 

Source
pub trait ExtensionError:
    Error
    + Send
    + Sync
    + 'static {
    // Required method
    fn code(&self) -> &'static str;

    // Provided methods
    fn status(&self) -> StatusCode { ... }
    fn is_retryable(&self) -> bool { ... }
    fn user_message(&self) -> String { ... }
    fn to_mcp_error(&self) -> McpErrorData { ... }
    fn to_api_error(&self) -> ApiError { ... }
}
Available on crate feature core only.
Expand description

Trait for extension error types to enable consistent error handling.

This trait provides a unified interface for extension errors that can be converted to both HTTP API responses and MCP protocol errors.

§Example

use systemprompt_traits::{ExtensionError, ApiError, McpErrorData};
use http::StatusCode;

#[derive(Debug, thiserror::Error)]
pub enum MyError {
    #[error("Resource not found: {0}")]
    NotFound(String),
    #[error("Invalid input: {0}")]
    InvalidInput(String),
}

impl ExtensionError for MyError {
    fn code(&self) -> &'static str {
        match self {
            Self::NotFound(_) => "NOT_FOUND",
            Self::InvalidInput(_) => "INVALID_INPUT",
        }
    }

    fn status(&self) -> StatusCode {
        match self {
            Self::NotFound(_) => StatusCode::NOT_FOUND,
            Self::InvalidInput(_) => StatusCode::BAD_REQUEST,
        }
    }
}

Required Methods§

Source

fn code(&self) -> &'static str

Machine-readable error code (e.g., CONTENT_NOT_FOUND).

Provided Methods§

Source

fn status(&self) -> StatusCode

HTTP status code for API responses.

Source

fn is_retryable(&self) -> bool

Whether this error is transient and operation should be retried.

Source

fn user_message(&self) -> String

User-facing message (defaults to Display impl).

Source

fn to_mcp_error(&self) -> McpErrorData

Convert to MCP protocol error format.

Source

fn to_api_error(&self) -> ApiError

Convert to API response error.

Implementors§