-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy patherrors.rs
104 lines (86 loc) · 2.74 KB
/
errors.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use thiserror::Error;
pub type BoxError = Box<dyn std::error::Error + Sync + Send>;
#[derive(Error, Debug)]
pub enum TransportError {
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("JSON serialization error: {0}")]
Json(#[from] serde_json::Error),
#[error("Invalid UTF-8 sequence: {0}")]
Utf8(#[from] std::string::FromUtf8Error),
#[error("Protocol error: {0}")]
Protocol(String),
#[error("Invalid message format: {0}")]
InvalidMessage(String),
}
#[derive(Error, Debug)]
pub enum ServerError {
#[error("Transport error: {0}")]
Transport(#[from] TransportError),
#[error("Service error: {0}")]
Service(String),
#[error("Internal error: {0}")]
Internal(String),
#[error("Request timed out")]
Timeout(#[from] tower::timeout::error::Elapsed),
}
#[derive(Error, Debug)]
pub enum RouterError {
#[error("Method not found: {0}")]
MethodNotFound(String),
#[error("Invalid parameters: {0}")]
InvalidParams(String),
#[error("Internal error: {0}")]
Internal(String),
#[error("Tool not found: {0}")]
ToolNotFound(String),
#[error("Resource not found: {0}")]
ResourceNotFound(String),
#[error("Not found: {0}")]
PromptNotFound(String),
}
impl From<RouterError> for mcp_spec::protocol::ErrorData {
fn from(err: RouterError) -> Self {
use mcp_spec::protocol::*;
match err {
RouterError::MethodNotFound(msg) => ErrorData {
code: METHOD_NOT_FOUND,
message: msg,
data: None,
},
RouterError::InvalidParams(msg) => ErrorData {
code: INVALID_PARAMS,
message: msg,
data: None,
},
RouterError::Internal(msg) => ErrorData {
code: INTERNAL_ERROR,
message: msg,
data: None,
},
RouterError::ToolNotFound(msg) => ErrorData {
code: INVALID_REQUEST,
message: msg,
data: None,
},
RouterError::ResourceNotFound(msg) => ErrorData {
code: INVALID_REQUEST,
message: msg,
data: None,
},
RouterError::PromptNotFound(msg) => ErrorData {
code: INVALID_REQUEST,
message: msg,
data: None,
},
}
}
}
impl From<mcp_spec::handler::ResourceError> for RouterError {
fn from(err: mcp_spec::handler::ResourceError) -> Self {
match err {
mcp_spec::handler::ResourceError::NotFound(msg) => RouterError::ResourceNotFound(msg),
_ => RouterError::Internal("Unknown resource error".to_string()),
}
}
}