agent_client_protocol_schema/
error.rs1use std::{fmt::Display, str};
14
15use schemars::{JsonSchema, Schema};
16use serde::{Deserialize, Serialize};
17
18use crate::IntoOption;
19
20pub type Result<T, E = Error> = std::result::Result<T, E>;
21
22#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
29#[non_exhaustive]
30pub struct Error {
31 pub code: ErrorCode,
34 pub message: String,
37 #[serde(skip_serializing_if = "Option::is_none")]
40 pub data: Option<serde_json::Value>,
41}
42
43impl Error {
44 pub fn new(code: i32, message: impl Into<String>) -> Self {
48 Error {
49 code: code.into(),
50 message: message.into(),
51 data: None,
52 }
53 }
54
55 #[must_use]
60 pub fn data(mut self, data: impl IntoOption<serde_json::Value>) -> Self {
61 self.data = data.into_option();
62 self
63 }
64
65 #[must_use]
67 pub fn parse_error() -> Self {
68 ErrorCode::ParseError.into()
69 }
70
71 #[must_use]
73 pub fn invalid_request() -> Self {
74 ErrorCode::InvalidRequest.into()
75 }
76
77 #[must_use]
79 pub fn method_not_found() -> Self {
80 ErrorCode::MethodNotFound.into()
81 }
82
83 #[must_use]
85 pub fn invalid_params() -> Self {
86 ErrorCode::InvalidParams.into()
87 }
88
89 #[must_use]
91 pub fn internal_error() -> Self {
92 ErrorCode::InternalError.into()
93 }
94
95 #[cfg(feature = "unstable_cancel_request")]
104 #[must_use]
105 pub fn request_cancelled() -> Self {
106 ErrorCode::RequestCancelled.into()
107 }
108
109 #[must_use]
111 pub fn auth_required() -> Self {
112 ErrorCode::AuthRequired.into()
113 }
114
115 #[must_use]
117 pub fn resource_not_found(uri: Option<String>) -> Self {
118 let err: Self = ErrorCode::ResourceNotFound.into();
119 if let Some(uri) = uri {
120 err.data(serde_json::json!({ "uri": uri }))
121 } else {
122 err
123 }
124 }
125
126 pub fn into_internal_error(err: impl std::error::Error) -> Self {
130 Error::internal_error().data(err.to_string())
131 }
132}
133
134#[derive(Clone, Copy, Deserialize, Eq, JsonSchema, PartialEq, Serialize, strum::Display)]
139#[cfg_attr(test, derive(strum::EnumIter))]
140#[serde(from = "i32", into = "i32")]
141#[schemars(!from, !into)]
142#[non_exhaustive]
143pub enum ErrorCode {
144 #[schemars(transform = error_code_transform)]
148 #[strum(to_string = "Parse error")]
149 ParseError, #[schemars(transform = error_code_transform)]
152 #[strum(to_string = "Invalid request")]
153 InvalidRequest, #[schemars(transform = error_code_transform)]
156 #[strum(to_string = "Method not found")]
157 MethodNotFound, #[schemars(transform = error_code_transform)]
160 #[strum(to_string = "Invalid params")]
161 InvalidParams, #[schemars(transform = error_code_transform)]
165 #[strum(to_string = "Internal error")]
166 InternalError, #[cfg(feature = "unstable_cancel_request")]
168 #[schemars(transform = error_code_transform)]
175 #[strum(to_string = "Request cancelled")]
176 RequestCancelled, #[schemars(transform = error_code_transform)]
181 #[strum(to_string = "Authentication required")]
182 AuthRequired, #[schemars(transform = error_code_transform)]
185 #[strum(to_string = "Resource not found")]
186 ResourceNotFound, #[schemars(untagged)]
190 #[strum(to_string = "Unknown error")]
191 Other(i32),
192}
193
194impl From<i32> for ErrorCode {
195 fn from(value: i32) -> Self {
196 match value {
197 -32700 => ErrorCode::ParseError,
198 -32600 => ErrorCode::InvalidRequest,
199 -32601 => ErrorCode::MethodNotFound,
200 -32602 => ErrorCode::InvalidParams,
201 -32603 => ErrorCode::InternalError,
202 #[cfg(feature = "unstable_cancel_request")]
203 -32800 => ErrorCode::RequestCancelled,
204 -32000 => ErrorCode::AuthRequired,
205 -32002 => ErrorCode::ResourceNotFound,
206 _ => ErrorCode::Other(value),
207 }
208 }
209}
210
211impl From<ErrorCode> for i32 {
212 fn from(value: ErrorCode) -> Self {
213 match value {
214 ErrorCode::ParseError => -32700,
215 ErrorCode::InvalidRequest => -32600,
216 ErrorCode::MethodNotFound => -32601,
217 ErrorCode::InvalidParams => -32602,
218 ErrorCode::InternalError => -32603,
219 #[cfg(feature = "unstable_cancel_request")]
220 ErrorCode::RequestCancelled => -32800,
221 ErrorCode::AuthRequired => -32000,
222 ErrorCode::ResourceNotFound => -32002,
223 ErrorCode::Other(value) => value,
224 }
225 }
226}
227
228impl std::fmt::Debug for ErrorCode {
229 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
230 write!(f, "{}: {self}", i32::from(*self))
231 }
232}
233
234fn error_code_transform(schema: &mut Schema) {
235 let name = schema
236 .get("const")
237 .expect("Unexpected schema for ErrorCode")
238 .as_str()
239 .expect("unexpected type for schema");
240 let code = match name {
241 "ParseError" => ErrorCode::ParseError,
242 "InvalidRequest" => ErrorCode::InvalidRequest,
243 "MethodNotFound" => ErrorCode::MethodNotFound,
244 "InvalidParams" => ErrorCode::InvalidParams,
245 "InternalError" => ErrorCode::InternalError,
246 #[cfg(feature = "unstable_cancel_request")]
247 "RequestCancelled" => ErrorCode::RequestCancelled,
248 "AuthRequired" => ErrorCode::AuthRequired,
249 "ResourceNotFound" => ErrorCode::ResourceNotFound,
250 _ => panic!("Unexpected error code name {name}"),
251 };
252 let mut description = schema
253 .get("description")
254 .expect("Missing description")
255 .as_str()
256 .expect("Unexpected type for description")
257 .to_owned();
258 schema.insert("title".into(), code.to_string().into());
259 description.insert_str(0, &format!("**{code}**: "));
260 schema.insert("description".into(), description.into());
261 schema.insert("const".into(), i32::from(code).into());
262 schema.insert("type".into(), "integer".into());
263 schema.insert("format".into(), "int32".into());
264}
265
266impl From<ErrorCode> for Error {
267 fn from(error_code: ErrorCode) -> Self {
268 Error::new(error_code.into(), error_code.to_string())
269 }
270}
271
272impl std::error::Error for Error {}
273
274impl Display for Error {
275 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
276 if self.message.is_empty() {
277 write!(f, "{}", i32::from(self.code))?;
278 } else {
279 write!(f, "{}", self.message)?;
280 }
281
282 if let Some(data) = &self.data {
283 let pretty = serde_json::to_string_pretty(data).unwrap_or_else(|_| data.to_string());
284 write!(f, ": {pretty}")?;
285 }
286
287 Ok(())
288 }
289}
290
291impl From<anyhow::Error> for Error {
292 fn from(error: anyhow::Error) -> Self {
293 match error.downcast::<Self>() {
294 Ok(error) => error,
295 Err(error) => Error::into_internal_error(&*error),
296 }
297 }
298}
299
300impl From<serde_json::Error> for Error {
301 fn from(error: serde_json::Error) -> Self {
302 Error::invalid_params().data(error.to_string())
303 }
304}
305
306#[cfg(test)]
307mod tests {
308 use strum::IntoEnumIterator;
309
310 use super::*;
311
312 #[test]
313 fn serialize_error_code() {
314 assert_eq!(
315 serde_json::from_value::<ErrorCode>(serde_json::json!(-32700)).unwrap(),
316 ErrorCode::ParseError
317 );
318 assert_eq!(
319 serde_json::to_value(ErrorCode::ParseError).unwrap(),
320 serde_json::json!(-32700)
321 );
322
323 assert_eq!(
324 serde_json::from_value::<ErrorCode>(serde_json::json!(1)).unwrap(),
325 ErrorCode::Other(1)
326 );
327 assert_eq!(
328 serde_json::to_value(ErrorCode::Other(1)).unwrap(),
329 serde_json::json!(1)
330 );
331 }
332
333 #[test]
334 fn serialize_error_code_equality() {
335 let _schema = schemars::schema_for!(ErrorCode);
337 for error in ErrorCode::iter() {
338 assert_eq!(
339 error,
340 serde_json::from_value(serde_json::to_value(error).unwrap()).unwrap()
341 );
342 }
343 }
344}