pub struct Error {
pub id: Uuid,
pub kind: ErrorKind,
pub message: String,
pub context: ErrorContext,
pub source: Option<Box<Error>>,
pub backtrace: Backtrace,
}Expand description
Comprehensive error type with rich context information
Fields§
§id: UuidUnique identifier for this error instance
kind: ErrorKindError classification
message: StringHuman-readable error message
context: ErrorContextAdditional contextual information
source: Option<Box<Error>>Optional source error that caused this error
backtrace: BacktraceStack trace information (when available)
Implementations§
Source§impl Error
impl Error
Sourcepub fn new(kind: ErrorKind, message: impl Into<String>) -> Box<Error>
pub fn new(kind: ErrorKind, message: impl Into<String>) -> Box<Error>
Create a new error with the specified kind and message
Sourcepub fn invalid_params(message: impl Into<String>) -> Box<Error>
pub fn invalid_params(message: impl Into<String>) -> Box<Error>
Create an invalid parameters error (MCP -32602)
This is the standard MCP error code for parameter validation failures, including missing required parameters, invalid types, out-of-range values, or any other parameter-related validation errors.
§Example
use turbomcp_protocol::Error;
let error = Error::invalid_params("Email must be valid");
assert_eq!(error.jsonrpc_error_code(), -32602);Sourcepub fn not_found(message: impl Into<String>) -> Box<Error>
👎Deprecated since 2.1.0: Use specific constructors: tool_not_found(), prompt_not_found(), or resource_not_found()
pub fn not_found(message: impl Into<String>) -> Box<Error>
Create a not found error
Sourcepub fn permission_denied(message: impl Into<String>) -> Box<Error>
pub fn permission_denied(message: impl Into<String>) -> Box<Error>
Create a permission denied error
Sourcepub fn rpc(code: i32, message: &str) -> Box<Error>
pub fn rpc(code: i32, message: &str) -> Box<Error>
Create a JSON-RPC error
Maps JSON-RPC error codes to appropriate ErrorKind variants to preserve semantic meaning. Special handling for MCP-specific codes like -1 (user rejection).
Create an unavailable error
Sourcepub fn external_service(message: impl Into<String>) -> Box<Error>
pub fn external_service(message: impl Into<String>) -> Box<Error>
Create an external service error
Sourcepub fn user_rejected(message: impl Into<String>) -> Box<Error>
pub fn user_rejected(message: impl Into<String>) -> Box<Error>
Create a user rejected error
Per MCP 2025-06-18 specification, this indicates a user explicitly rejected a request (e.g., declined a sampling request). This is a permanent failure that should not be retried.
Sourcepub fn handler(message: impl Into<String>) -> Box<Error>
👎Deprecated since 2.1.0: Use specific error constructors: tool_not_found(), tool_execution_failed(), etc.
pub fn handler(message: impl Into<String>) -> Box<Error>
Create a handler error - for compatibility with macro-generated code
Sourcepub fn tool_not_found(tool_name: impl Into<String>) -> Box<Error>
pub fn tool_not_found(tool_name: impl Into<String>) -> Box<Error>
Create a tool not found error (MCP error code -32001)
§Example
use turbomcp_protocol::Error;
let error = Error::tool_not_found("calculate");
assert_eq!(error.jsonrpc_error_code(), -32001);Sourcepub fn tool_execution_failed(
tool_name: impl Into<String>,
reason: impl Into<String>,
) -> Box<Error>
pub fn tool_execution_failed( tool_name: impl Into<String>, reason: impl Into<String>, ) -> Box<Error>
Create a tool execution failed error (MCP error code -32002)
§Example
use turbomcp_protocol::Error;
let error = Error::tool_execution_failed("calculate", "Division by zero");
assert_eq!(error.jsonrpc_error_code(), -32002);Sourcepub fn prompt_not_found(prompt_name: impl Into<String>) -> Box<Error>
pub fn prompt_not_found(prompt_name: impl Into<String>) -> Box<Error>
Create a prompt not found error (MCP error code -32003)
§Example
use turbomcp_protocol::Error;
let error = Error::prompt_not_found("code_review");
assert_eq!(error.jsonrpc_error_code(), -32003);Sourcepub fn resource_not_found(uri: impl Into<String>) -> Box<Error>
pub fn resource_not_found(uri: impl Into<String>) -> Box<Error>
Create a resource not found error (MCP error code -32004)
§Example
use turbomcp_protocol::Error;
let error = Error::resource_not_found("file:///docs/api.md");
assert_eq!(error.jsonrpc_error_code(), -32004);Sourcepub fn resource_access_denied(
uri: impl Into<String>,
reason: impl Into<String>,
) -> Box<Error>
pub fn resource_access_denied( uri: impl Into<String>, reason: impl Into<String>, ) -> Box<Error>
Create a resource access denied error (MCP error code -32005)
§Example
use turbomcp_protocol::Error;
let error = Error::resource_access_denied("file:///etc/passwd", "Path outside allowed directory");
assert_eq!(error.jsonrpc_error_code(), -32005);Sourcepub fn capability_not_supported(capability: impl Into<String>) -> Box<Error>
pub fn capability_not_supported(capability: impl Into<String>) -> Box<Error>
Create a capability not supported error (MCP error code -32006)
§Example
use turbomcp_protocol::Error;
let error = Error::capability_not_supported("sampling");
assert_eq!(error.jsonrpc_error_code(), -32006);Sourcepub fn protocol_version_mismatch(
client_version: impl Into<String>,
server_version: impl Into<String>,
) -> Box<Error>
pub fn protocol_version_mismatch( client_version: impl Into<String>, server_version: impl Into<String>, ) -> Box<Error>
Create a protocol version mismatch error (MCP error code -32007)
§Example
use turbomcp_protocol::Error;
let error = Error::protocol_version_mismatch("2024-11-05", "2025-06-18");
assert_eq!(error.jsonrpc_error_code(), -32007);Sourcepub fn server_overloaded() -> Box<Error>
pub fn server_overloaded() -> Box<Error>
Create a server overloaded error (MCP error code -32010)
§Example
use turbomcp_protocol::Error;
let error = Error::server_overloaded();
assert_eq!(error.jsonrpc_error_code(), -32010);Sourcepub fn with_context(
self: Box<Error>,
key: impl Into<String>,
value: impl Into<Value>,
) -> Box<Error>
pub fn with_context( self: Box<Error>, key: impl Into<String>, value: impl Into<Value>, ) -> Box<Error>
Add context to this error
Sourcepub fn with_operation(
self: Box<Error>,
operation: impl Into<String>,
) -> Box<Error>
pub fn with_operation( self: Box<Error>, operation: impl Into<String>, ) -> Box<Error>
Set the operation being performed
Sourcepub fn with_component(
self: Box<Error>,
component: impl Into<String>,
) -> Box<Error>
pub fn with_component( self: Box<Error>, component: impl Into<String>, ) -> Box<Error>
Set the component where error occurred
Sourcepub fn with_request_id(
self: Box<Error>,
request_id: impl Into<String>,
) -> Box<Error>
pub fn with_request_id( self: Box<Error>, request_id: impl Into<String>, ) -> Box<Error>
Set the request ID for tracing
Sourcepub fn with_user_id(self: Box<Error>, user_id: impl Into<String>) -> Box<Error>
pub fn with_user_id(self: Box<Error>, user_id: impl Into<String>) -> Box<Error>
Set the user ID
Sourcepub fn with_retry_info(self: Box<Error>, retry_info: RetryInfo) -> Box<Error>
pub fn with_retry_info(self: Box<Error>, retry_info: RetryInfo) -> Box<Error>
Add retry information
Sourcepub fn with_source(self: Box<Error>, source: Box<Error>) -> Box<Error>
pub fn with_source(self: Box<Error>, source: Box<Error>) -> Box<Error>
Chain this error with a source error
Sourcepub const fn is_retryable(&self) -> bool
pub const fn is_retryable(&self) -> bool
Check if this error is retryable based on its kind
Sourcepub const fn is_temporary(&self) -> bool
pub const fn is_temporary(&self) -> bool
Check if this error indicates a temporary failure
Sourcepub const fn http_status_code(&self) -> u16
pub const fn http_status_code(&self) -> u16
Get the HTTP status code equivalent for this error
Sourcepub const fn jsonrpc_error_code(&self) -> i32
pub const fn jsonrpc_error_code(&self) -> i32
Convert to a JSON-RPC error code per MCP 2025-06-18 specification
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Error
impl<'de> Deserialize<'de> for Error
Source§fn deserialize<D>(
deserializer: D,
) -> Result<Error, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<Error, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
Source§impl Error for Error
impl Error for Error
Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0§fn description(&self) -> &str
fn description(&self) -> &str
Source§impl Serialize for Error
impl Serialize for Error
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
Auto Trait Implementations§
impl !Freeze for Error
impl RefUnwindSafe for Error
impl Send for Error
impl Sync for Error
impl Unpin for Error
impl UnwindSafe for Error
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)