feat: ToolError migration to ErrorData (#4051)
This commit is contained in:
@@ -1,22 +1,7 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
#[allow(unused_imports)] // this is used in schema below
|
||||
use serde_json::{json, Value};
|
||||
use rmcp::model::{ErrorCode, ErrorData};
|
||||
use thiserror::Error;
|
||||
|
||||
#[non_exhaustive]
|
||||
#[derive(Error, Debug, Clone, Deserialize, Serialize, PartialEq)]
|
||||
pub enum ToolError {
|
||||
#[error("Invalid parameters: {0}")]
|
||||
InvalidParameters(String),
|
||||
#[error("Execution failed: {0}")]
|
||||
ExecutionError(String),
|
||||
#[error("Schema error: {0}")]
|
||||
SchemaError(String),
|
||||
#[error("Tool not found: {0}")]
|
||||
NotFound(String),
|
||||
}
|
||||
|
||||
pub type ToolResult<T> = std::result::Result<T, ToolError>;
|
||||
pub type ToolResult<T> = std::result::Result<T, ErrorData>;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum ResourceError {
|
||||
@@ -36,31 +21,43 @@ pub enum PromptError {
|
||||
NotFound(String),
|
||||
}
|
||||
|
||||
/// Helper function to require a string, returning a ToolError
|
||||
/// Helper function to require a string, returning an ErrorData
|
||||
pub fn require_str_parameter<'a>(
|
||||
v: &'a serde_json::Value,
|
||||
name: &str,
|
||||
) -> Result<&'a str, ToolError> {
|
||||
let v = v
|
||||
.get(name)
|
||||
.ok_or_else(|| ToolError::InvalidParameters(format!("The parameter {name} is required")))?;
|
||||
) -> Result<&'a str, ErrorData> {
|
||||
let v = v.get(name).ok_or_else(|| {
|
||||
ErrorData::new(
|
||||
ErrorCode::INVALID_PARAMS,
|
||||
format!("The parameter {name} is required"),
|
||||
None,
|
||||
)
|
||||
})?;
|
||||
match v.as_str() {
|
||||
Some(r) => Ok(r),
|
||||
None => Err(ToolError::InvalidParameters(format!(
|
||||
"The parameter {name} must be a string"
|
||||
))),
|
||||
None => Err(ErrorData::new(
|
||||
ErrorCode::INVALID_PARAMS,
|
||||
format!("The parameter {name} must be a string"),
|
||||
None,
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Helper function to require a u64, returning a ToolError
|
||||
pub fn require_u64_parameter(v: &serde_json::Value, name: &str) -> Result<u64, ToolError> {
|
||||
let v = v
|
||||
.get(name)
|
||||
.ok_or_else(|| ToolError::InvalidParameters(format!("The parameter {name} is required")))?;
|
||||
/// Helper function to require a u64, returning an ErrorData
|
||||
pub fn require_u64_parameter(v: &serde_json::Value, name: &str) -> Result<u64, ErrorData> {
|
||||
let v = v.get(name).ok_or_else(|| {
|
||||
ErrorData::new(
|
||||
ErrorCode::INVALID_PARAMS,
|
||||
format!("The parameter {name} is required"),
|
||||
None,
|
||||
)
|
||||
})?;
|
||||
match v.as_u64() {
|
||||
Some(r) => Ok(r),
|
||||
None => Err(ToolError::InvalidParameters(format!(
|
||||
"The parameter {name} must be a number"
|
||||
))),
|
||||
None => Err(ErrorData::new(
|
||||
ErrorCode::INVALID_PARAMS,
|
||||
format!("The parameter {name} is required"),
|
||||
None,
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,4 +2,4 @@ pub mod handler;
|
||||
pub mod tool;
|
||||
pub use tool::{Tool, ToolCall};
|
||||
pub mod protocol;
|
||||
pub use handler::{ToolError, ToolResult};
|
||||
pub use handler::ToolResult;
|
||||
|
||||
Reference in New Issue
Block a user