vv_agent/app_server/protocol/
errors.rs1use serde_json::Value;
2
3use super::jsonrpc::{JsonRpcError, JsonRpcErrorBody, RequestId};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum AppServerErrorCode {
7 ParseError,
8 ServerOverloaded,
9 InvalidRequest,
10 MethodNotFound,
11 InvalidParams,
12 InternalError,
13 NotInitialized,
14 AlreadyInitialized,
15 ThreadNotFound,
16 ThreadArchived,
17 ActiveTurnNotFound,
18 TurnIdMismatch,
19 ExperimentalApiRequired,
20 UnsupportedMethod,
21}
22
23impl AppServerErrorCode {
24 pub fn code(self) -> i64 {
25 match self {
26 Self::ParseError => -32700,
27 Self::ServerOverloaded => -32001,
28 Self::InvalidRequest => -32600,
29 Self::MethodNotFound => -32601,
30 Self::InvalidParams => -32602,
31 Self::InternalError => -32603,
32 Self::NotInitialized => -32010,
33 Self::AlreadyInitialized => -32011,
34 Self::ThreadNotFound => -32020,
35 Self::ThreadArchived => -32021,
36 Self::ActiveTurnNotFound => -32030,
37 Self::TurnIdMismatch => -32031,
38 Self::ExperimentalApiRequired => -32012,
39 Self::UnsupportedMethod => -32013,
40 }
41 }
42}
43
44#[derive(Debug, Clone, PartialEq)]
45pub struct AppServerError {
46 code: AppServerErrorCode,
47 message: String,
48 data: Option<Value>,
49}
50
51impl AppServerError {
52 pub fn new(code: AppServerErrorCode, message: impl Into<String>) -> Self {
53 Self {
54 code,
55 message: message.into(),
56 data: None,
57 }
58 }
59
60 pub fn parse_error(message: impl Into<String>) -> Self {
61 Self::new(AppServerErrorCode::ParseError, message)
62 }
63
64 pub fn invalid_params(message: impl Into<String>) -> Self {
65 Self::new(AppServerErrorCode::InvalidParams, message)
66 }
67
68 pub fn invalid_request(message: impl Into<String>) -> Self {
69 Self::new(AppServerErrorCode::InvalidRequest, message)
70 }
71
72 pub fn internal(message: impl Into<String>) -> Self {
73 Self::new(AppServerErrorCode::InternalError, message)
74 }
75
76 pub fn not_initialized() -> Self {
77 Self::new(AppServerErrorCode::NotInitialized, "Not initialized")
78 }
79
80 pub fn already_initialized() -> Self {
81 Self::new(
82 AppServerErrorCode::AlreadyInitialized,
83 "Already initialized",
84 )
85 }
86
87 pub fn thread_not_found() -> Self {
88 Self::new(AppServerErrorCode::ThreadNotFound, "Thread not found")
89 }
90
91 pub fn thread_archived() -> Self {
92 Self::new(AppServerErrorCode::ThreadArchived, "Thread archived")
93 }
94
95 pub fn active_turn_not_found() -> Self {
96 Self::new(
97 AppServerErrorCode::ActiveTurnNotFound,
98 "Active turn not found",
99 )
100 }
101
102 pub fn turn_id_mismatch() -> Self {
103 Self::new(AppServerErrorCode::TurnIdMismatch, "Turn id mismatch")
104 }
105
106 pub fn server_overloaded() -> Self {
107 Self::new(
108 AppServerErrorCode::ServerOverloaded,
109 "Server overloaded; retry later.",
110 )
111 }
112
113 pub fn unsupported_method(method: impl Into<String>) -> Self {
114 let method = method.into();
115 Self::new(
116 AppServerErrorCode::UnsupportedMethod,
117 format!("Unsupported App Server method: {method}"),
118 )
119 .with_data(serde_json::json!({ "method": method }))
120 }
121
122 pub fn with_data(mut self, data: Value) -> Self {
123 self.data = Some(data);
124 self
125 }
126
127 pub fn code(&self) -> AppServerErrorCode {
128 self.code
129 }
130
131 pub fn message(&self) -> &str {
132 &self.message
133 }
134
135 pub fn into_json_rpc_error(self, id: RequestId) -> JsonRpcError {
136 JsonRpcError {
137 id,
138 error: JsonRpcErrorBody {
139 code: self.code.code(),
140 message: self.message,
141 data: self.data,
142 },
143 }
144 }
145}