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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
use std::fmt::Display;
use thiserror::Error;
use crate::transport::TransportError;
/// Result type returned by serving and Transport entry points.
pub type Result<T> = std::result::Result<T, Error>;
/// A terminal framework or Transport failure while serving a connection.
#[derive(Debug, Error)]
pub enum Error {
/// A protocol error produced while handling an incoming call.
#[error(transparent)]
Lsp(#[from] LspError),
/// The underlying Transport failed.
#[error(transparent)]
Transport(#[from] TransportError),
/// Native serving was attempted without a running Tokio runtime. The
/// framework never starts a runtime implicitly (ADR 0020): start one —
/// for example with `#[tokio::main]` — before serving the connection.
#[cfg(not(target_arch = "wasm32"))]
#[error("no Tokio runtime is running; start one before serving the connection")]
RuntimeRequired,
}
/// A failure to enqueue a typed server-to-client operation.
#[derive(Debug, Error)]
pub enum ClientError {
/// The typed parameters could not be encoded as JSON.
#[error("failed to serialize client parameters: {0}")]
Serialize(#[source] serde_json::Error),
/// The connection began closing before the operation could be enqueued.
#[error("client connection is closed")]
ConnectionClosed,
/// The engine-owned outbound queue is closed.
#[error("client outbound queue is closed")]
OutboundClosed,
/// The connection's outbound message-count or encoded-byte budget is full.
#[error("client outbound queue capacity is exhausted")]
OutboundOverloaded,
/// The session was cancelled before the peer answered the request.
#[error("client request was cancelled")]
Cancelled,
/// The peer did not answer before the connection's outbound-request
/// deadline expired.
#[error("client request timed out")]
Timeout,
/// The connection exhausted the positive outbound request-ID or
/// progress-token space; no further server-to-client requests or progress
/// tokens can be allocated on this connection.
#[error("outbound request ID space exhausted")]
IdExhausted,
/// The remote peer returned a JSON-RPC error response. The original
/// error's code, message, and optional data are preserved.
#[error("remote error (code {code}): {message}", code = .0.code, message = .0.message)]
Remote(crate::raw::JsonRpcError),
/// The remote peer returned a success result that could not be decoded
/// into the expected type.
#[error("failed to deserialize client response: {0}")]
Deserialize(#[source] serde_json::Error),
/// A named helper rejected its own parameters before anything was
/// encoded or enqueued: no request or notification was sent.
#[error("invalid helper parameters: {0}")]
InvalidHelperParams(String),
/// A work-done progress lifecycle failure from a
/// [`ProgressHandle`](crate::ProgressHandle) operation.
#[error(transparent)]
Progress(#[from] ProgressError),
}
/// A failure of the connection-scoped work-done progress lifecycle
/// ([`Client::begin_progress`](crate::Client::begin_progress) and the
/// resulting [`ProgressHandle`](crate::ProgressHandle)).
///
/// These failures describe the handle's own state, not transport or remote
/// failures: enqueue and response failures surface as the other
/// [`ClientError`] variants.
#[derive(Debug, Error, PartialEq, Eq)]
pub enum ProgressError {
/// The handle's progress was already ended; an ended handle reports and
/// ends nothing further.
#[error("progress handle has already ended")]
AlreadyEnded,
/// The handle's cancellation token was cancelled; a cancelled handle
/// sends no further report notifications.
#[error("progress was cancelled")]
Cancelled,
/// The handle's token is not active on this connection anymore (for
/// example because the handle was dropped or the registry entry was
/// removed by another path).
#[error("progress token is not active on this connection")]
UnknownToken,
/// A report percentage outside the inclusive range 0 through 100.
#[error("progress percentage {0} is outside the range 0..=100")]
InvalidPercentage(u32),
}
/// A configuration error surfaced by [`ServerBuilder::build`](crate::ServerBuilder::build).
///
/// `BuildError` is deliberately distinct from [`LspError`]: it names a static
/// registration mistake the developer must fix before the server ever runs,
/// never a value that goes on the wire. `build()` performs no I/O and returns
/// this before any transport is touched.
#[derive(Debug, Error, PartialEq, Eq)]
#[non_exhaustive]
pub enum BuildError {
/// More than one connection-level error hook was registered.
#[error("connection error hook registered more than once")]
DuplicateErrorHook,
/// Two handlers were registered for the same request method.
#[error("duplicate handler registered for method `{0}`")]
DuplicateMethod(String),
/// A custom request or notification was registered under a method the
/// framework owns (`initialize`, `shutdown`, `exit`, `initialized`,
/// `$/cancelRequest`).
#[error("method `{0}` is reserved by the framework and cannot be overridden")]
ReservedMethod(String),
/// Two handlers were registered for the same command name.
#[error("duplicate handler registered for command `{0}`")]
DuplicateCommand(String),
/// A command was registered with an empty name; a command name is the key
/// the editor sends on `workspace/executeCommand`, so it cannot be empty.
#[error("a command name cannot be empty")]
EmptyCommandName,
/// One or more commands were registered alongside an explicit
/// `workspace/executeCommand` request handler. Commands already dispatch
/// beneath that method, so a user handler for it would shadow them.
#[error(
"commands conflict with an explicit `workspace/executeCommand` handler; \
register either commands or the raw method, not both"
)]
ExecuteCommandConflict,
/// Two contributions disagreed on a capability field that must be singular
/// within its family, or a dependent feature was registered without the
/// base its family requires (ADR 0017). Capability construction never
/// resolves such a clash by last-write-wins; it fails the build instead.
#[error("conflicting contributions for capability field `{field}`")]
ConflictingCapability {
/// The singular `ServerCapabilities` field with conflicting inputs.
field: &'static str,
},
/// `configure_initialize` was supplied more than once. There is exactly one
/// initialization-dependent registration transaction (ADR 0017).
#[error("`configure_initialize` may only be supplied once")]
DuplicateConfigureInitialize,
/// A lifecycle hook (`on_initialize`, …) was supplied more than once. Each
/// hook has at most one registration (ADR 0018).
#[error("lifecycle hook `{0}` may only be supplied once")]
DuplicateLifecycleHook(&'static str),
/// A zero concurrency limit could never admit a user call.
#[error("concurrency limit must be greater than zero")]
InvalidConcurrencyLimit,
/// A zero outbound warning threshold could never be crossed from below.
#[error("outbound warning threshold must be greater than zero")]
InvalidOutboundWarningThreshold,
/// A connection resource budget or enabled deadline was zero.
#[error("resource policy `{field}` must be greater than zero when enabled")]
InvalidResourcePolicy {
/// The invalid [`ResourcePolicy`](crate::ResourcePolicy) field.
field: crate::ResourcePolicyField,
},
}
/// A JSON-RPC/LSP error suitable for returning from a handler.
#[derive(Debug, Error)]
pub enum LspError {
/// An unexpected server failure (`-32603`).
#[error("internal error: {0}")]
Internal(String),
/// Invalid method parameters (`-32602`).
#[error("invalid params: {0}")]
InvalidParams(String),
/// No handler exists for the method (`-32601`).
#[error("method not found: {0}")]
MethodNotFound(String),
/// The peer cancelled the request (`-32800`).
#[error("request cancelled")]
RequestCancelled,
/// The requested content changed before completion (`-32801`).
#[error("content modified")]
ContentModified,
/// A request arrived before initialization completed (`-32002`).
#[error("server not initialized")]
ServerNotInitialized,
/// The incoming JSON-RPC request is invalid (`-32600`).
#[error("invalid request: {0}")]
InvalidRequest(String),
/// An application-defined server error.
#[error("{message}")]
ServerError {
/// Application-defined JSON-RPC error code.
code: i32,
/// Human-readable error message.
message: String,
/// Optional structured error data.
data: Option<serde_json::Value>,
},
}
impl LspError {
/// Construct an internal-error response from a displayable value.
pub fn internal(e: impl Display) -> Self {
Self::Internal(e.to_string())
}
/// Construct an invalid-params response from a displayable value.
pub fn invalid_params(e: impl Display) -> Self {
Self::InvalidParams(e.to_string())
}
/// Construct an invalid-request response from a displayable value.
pub fn invalid_request(e: impl Display) -> Self {
Self::InvalidRequest(e.to_string())
}
/// The JSON-RPC/LSP numeric error code.
pub fn code(&self) -> i32 {
match self {
Self::Internal(_) => -32603,
Self::InvalidParams(_) => -32602,
Self::MethodNotFound(_) => -32601,
Self::RequestCancelled => -32800,
Self::ContentModified => -32801,
Self::ServerNotInitialized => -32002,
Self::InvalidRequest(_) => -32600,
Self::ServerError { code, .. } => *code,
}
}
/// The human-readable error message.
pub fn message(&self) -> String {
match self {
Self::Internal(m)
| Self::InvalidParams(m)
| Self::MethodNotFound(m)
| Self::InvalidRequest(m) => m.clone(),
Self::RequestCancelled => "request cancelled".to_string(),
Self::ContentModified => "content modified".to_string(),
Self::ServerNotInitialized => "server not initialized".to_string(),
Self::ServerError { message, .. } => message.clone(),
}
}
/// Optional structured error data for an application-defined server error.
pub fn data(&self) -> Option<&serde_json::Value> {
match self {
Self::ServerError { data, .. } => data.as_ref(),
_ => None,
}
}
}