csskit_lsp/jsonrpc/error_code.rs
1use serde::{Deserialize, Serialize};
2
3/// An ErrorCode representing either a [JSON-RPC Error Code](https://www.jsonrpc.org/specification#error_object) or [LSP
4/// defined error
5/// code](https://microsoft.github.io/language-server-protocol/specifications/specification-3-16/#errorCodes).
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Deserialize, Serialize)]
7#[serde(into = "i32", from = "i32")]
8pub enum ErrorCode {
9 // JSON RPC Generic
10 /// Invalid JSON was received by the server. An error occurred on the server while parsing the JSON text.
11 /// [JSON-RPC defined](https://www.jsonrpc.org/specification#error_object). `-32700`.
12 ParseError,
13
14 /// The JSON sent is not a valid Request object.
15 /// [JSON-RPC defined](https://www.jsonrpc.org/specification#error_object). `-32600`.
16 InvalidRequest,
17
18 /// The method does not exist / is not available.
19 /// [JSON-RPC defined](https://www.jsonrpc.org/specification#error_object). `-32601`.
20 MethodNotFound,
21
22 /// Invalid method parameter(s).
23 /// [JSON-RPC defined](https://www.jsonrpc.org/specification#error_object). `-32602`.
24 InvalidParams,
25
26 /// Internal JSON-RPC error.
27 /// [JSON-RPC defined](https://www.jsonrpc.org/specification#error_object). `-32603`.
28 InternalError,
29
30 /// [JSON RPC Reserved Range Start](https://www.jsonrpc.org/specification#error_object).
31 /// Reserved for implementation-defined server-errors. (`-32000` to `-32099`).
32 ReservedErrorStart,
33
34 /// Error code indicating that a server received a notification or
35 /// request before the server has received the `initialize` request.
36 /// [LSP defined](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/#errorCodes). `-32002`.
37 ServerNotInitialized,
38
39 /// [LSP defined](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/#errorCodes). `-32001`.
40 UnknownErrorCode,
41
42 /// [JSON RPC Reserved Range End](https://www.jsonrpc.org/specification#error_object).
43 /// Reserved for implementation-defined server-errors. (`-32000` to `-32099`).
44 ReservedErrorEnd,
45
46 /// [LSP Reserved Range Start](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/#errorCodes).
47 /// This is the start range of LSP reserved error codes. It doesn't denote a real error code. (`-32899` to `-32800`)
48 LspReservedErrorStart,
49
50 /// The server detected that the content of a document got modified outside normal conditions. A server should NOT
51 /// send this error code if it detects a content change in its unprocessed messages. The result even computed on
52 /// an older state might still be useful for the client.
53 ///
54 /// If a client decides that a result is not of any use anymore the client should cancel the request.
55 ///
56 /// [LSP defined](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/#errorCodes). `-32801`.
57 ContentModified,
58
59 /// The client has canceled a request and a server has detected the cancel.
60 ///
61 /// [LSP defined](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/#errorCodes). `-32800`.
62 RequestCancelled,
63
64 /// [LSP Reserved Range Start](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/#errorCodes).
65 /// This is the start range of LSP reserved error codes. It doesn't denote a real error code. (`-32899` to `-32800`)
66 LspReservedErrorEnd,
67
68 /// The error code is not known.
69 Unknown(i32),
70}
71
72// https://microsoft.github.io/language-server-protocol/specifications/specification-3-16/#responseMessage
73impl From<ErrorCode> for i32 {
74 fn from(value: ErrorCode) -> Self {
75 match value {
76 ErrorCode::ParseError => -32700,
77 ErrorCode::InvalidRequest => -32600,
78 ErrorCode::MethodNotFound => -32601,
79 ErrorCode::InvalidParams => -32602,
80 ErrorCode::InternalError => -32603,
81
82 // JSON RPC Reserved Range
83 ErrorCode::ReservedErrorStart => -32099,
84 ErrorCode::ServerNotInitialized => -32002,
85 ErrorCode::UnknownErrorCode => -32001,
86 ErrorCode::ReservedErrorEnd => -32000,
87
88 // LSP Reserved Range
89 ErrorCode::LspReservedErrorStart => -32899,
90 ErrorCode::ContentModified => -32801,
91 ErrorCode::RequestCancelled => -32800,
92 ErrorCode::LspReservedErrorEnd => -32800,
93
94 ErrorCode::Unknown(code) => code,
95 }
96 }
97}
98
99impl From<i32> for ErrorCode {
100 fn from(value: i32) -> Self {
101 match value {
102 -32700 => ErrorCode::ParseError,
103 -32600 => ErrorCode::InvalidRequest,
104 -32601 => ErrorCode::MethodNotFound,
105 -32602 => ErrorCode::InvalidParams,
106 -32603 => ErrorCode::InternalError,
107 // JSON RPC Reserved Range
108 -32099 => ErrorCode::ReservedErrorStart,
109 -32002 => ErrorCode::ServerNotInitialized,
110 -32001 => ErrorCode::UnknownErrorCode,
111 -32000 => ErrorCode::ReservedErrorEnd,
112
113 // LSP Reserved Range
114 -32899 => ErrorCode::LspReservedErrorStart,
115 -32801 => ErrorCode::ContentModified,
116 -32800 => ErrorCode::RequestCancelled,
117
118 code => ErrorCode::Unknown(code),
119 }
120 }
121}