agent_client_protocol_schema/
error.rs

1//! Error handling for the Agent Client Protocol.
2//!
3//! This module provides error types and codes following the JSON-RPC 2.0 specification,
4//! with additional protocol-specific error codes for authentication and other ACP-specific scenarios.
5//!
6//! All methods in the protocol follow standard JSON-RPC 2.0 error handling:
7//! - Successful responses include a `result` field
8//! - Errors include an `error` object with `code` and `message`
9//! - Notifications never receive responses (success or error)
10//!
11//! See: [Error Handling](https://agentclientprotocol.com/protocol/overview#error-handling)
12
13use 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/// JSON-RPC error object.
23///
24/// Represents an error that occurred during method execution, following the
25/// JSON-RPC 2.0 error object specification with optional additional data.
26///
27/// See protocol docs: [JSON-RPC Error Object](https://www.jsonrpc.org/specification#error_object)
28#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
29#[non_exhaustive]
30pub struct Error {
31    /// A number indicating the error type that occurred.
32    /// This must be an integer as defined in the JSON-RPC specification.
33    pub code: ErrorCode,
34    /// A string providing a short description of the error.
35    /// The message should be limited to a concise single sentence.
36    pub message: String,
37    /// Optional primitive or structured value that contains additional information about the error.
38    /// This may include debugging information or context-specific details.
39    #[serde(skip_serializing_if = "Option::is_none")]
40    pub data: Option<serde_json::Value>,
41}
42
43impl Error {
44    /// Creates a new error with the given code and message.
45    ///
46    /// The code parameter can be an `ErrorCode` constant or a tuple of (code, message).
47    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    /// Adds additional data to the error.
56    ///
57    /// This method is chainable and allows attaching context-specific information
58    /// to help with debugging or provide more details about the error.
59    #[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    /// Invalid JSON was received by the server. An error occurred on the server while parsing the JSON text.
66    #[must_use]
67    pub fn parse_error() -> Self {
68        ErrorCode::ParseError.into()
69    }
70
71    /// The JSON sent is not a valid Request object.
72    #[must_use]
73    pub fn invalid_request() -> Self {
74        ErrorCode::InvalidRequest.into()
75    }
76
77    /// The method does not exist / is not available.
78    #[must_use]
79    pub fn method_not_found() -> Self {
80        ErrorCode::MethodNotFound.into()
81    }
82
83    /// Invalid method parameter(s).
84    #[must_use]
85    pub fn invalid_params() -> Self {
86        ErrorCode::InvalidParams.into()
87    }
88
89    /// Internal JSON-RPC error.
90    #[must_use]
91    pub fn internal_error() -> Self {
92        ErrorCode::InternalError.into()
93    }
94
95    /// **UNSTABLE**
96    ///
97    /// This capability is not part of the spec yet, and may be removed or changed at any point.
98    ///
99    /// Request was cancelled.
100    ///
101    /// Execution of the method was aborted either due to a cancellation request from the caller
102    /// or because of resource constraints or shutdown.
103    #[cfg(feature = "unstable_cancel_request")]
104    #[must_use]
105    pub fn request_cancelled() -> Self {
106        ErrorCode::RequestCancelled.into()
107    }
108
109    /// Authentication required.
110    #[must_use]
111    pub fn auth_required() -> Self {
112        ErrorCode::AuthRequired.into()
113    }
114
115    /// A given resource, such as a file, was not found.
116    #[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    /// Converts a standard error into an internal JSON-RPC error.
127    ///
128    /// The error's string representation is included as additional data.
129    pub fn into_internal_error(err: impl std::error::Error) -> Self {
130        Error::internal_error().data(err.to_string())
131    }
132}
133
134/// Predefined error codes for common JSON-RPC and ACP-specific errors.
135///
136/// These codes follow the JSON-RPC 2.0 specification for standard errors
137/// and use the reserved range (-32000 to -32099) for protocol-specific errors.
138#[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    // Standard errors
145    /// Invalid JSON was received by the server.
146    /// An error occurred on the server while parsing the JSON text.
147    #[schemars(transform = error_code_transform)]
148    #[strum(to_string = "Parse error")]
149    ParseError, // -32700
150    /// The JSON sent is not a valid Request object.
151    #[schemars(transform = error_code_transform)]
152    #[strum(to_string = "Invalid request")]
153    InvalidRequest, // -32600
154    /// The method does not exist or is not available.
155    #[schemars(transform = error_code_transform)]
156    #[strum(to_string = "Method not found")]
157    MethodNotFound, // -32601
158    /// Invalid method parameter(s).
159    #[schemars(transform = error_code_transform)]
160    #[strum(to_string = "Invalid params")]
161    InvalidParams, // -32602
162    /// Internal JSON-RPC error.
163    /// Reserved for implementation-defined server errors.
164    #[schemars(transform = error_code_transform)]
165    #[strum(to_string = "Internal error")]
166    InternalError, // -32603
167    #[cfg(feature = "unstable_cancel_request")]
168    /// **UNSTABLE**
169    ///
170    /// This capability is not part of the spec yet, and may be removed or changed at any point.
171    ///
172    /// Execution of the method was aborted either due to a cancellation request from the caller or
173    /// because of resource constraints or shutdown.
174    #[schemars(transform = error_code_transform)]
175    #[strum(to_string = "Request cancelled")]
176    RequestCancelled, // -32800
177
178    // Custom errors
179    /// Authentication is required before this operation can be performed.
180    #[schemars(transform = error_code_transform)]
181    #[strum(to_string = "Authentication required")]
182    AuthRequired, // -32000
183    /// A given resource, such as a file, was not found.
184    #[schemars(transform = error_code_transform)]
185    #[strum(to_string = "Resource not found")]
186    ResourceNotFound, // -32002
187
188    /// Other undefined error code.
189    #[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        // Make sure this doesn't panic
336        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}