lsp_primitives/json_rpc/
error.rs

1use crate::json_rpc::{JsonRpcId, JsonRpcResponseFailure};
2use serde::{Deserialize, Serialize};
3
4pub mod codes {
5    pub const PARSE_ERROR_CODE: i64 = -32700;
6    pub const PARSE_ERROR_MSG: &str = "Parse Error";
7
8    pub const INVALID_REQUEST_CODE: i64 = -36200;
9    pub const INVALID_REQUEST_MSG: &str = "Invalid Request";
10
11    pub const METHOD_NOT_FOUND_CODE: i64 = -32601;
12    pub const METHOD_NOT_FOUND_MSG: &str = "Method not found";
13
14    pub const INVALID_PARAMS_CODE: i64 = -32602;
15    pub const INVALID_PARAMS_MSG: &str = "Invalid Params";
16
17    pub const INTERNAL_ERROR_CODE: i64 = -32603;
18    pub const INTERNAL_ERROR_MSG: &str = "Internal Error";
19
20    pub const NOT_FOUND_CODE: i64 = 404;
21    pub const NOT_FOUND_MSG: &str = "Not Found";
22
23    pub const OPTIONS_MISMATCH_CODE: i64 = 1000;
24    pub const OPTIONS_MISMATCH_MSG: &str = "Options mismatch";
25
26    pub const CLIENT_REJECTED_CODE: i64 = 1001;
27    pub const CLIENT_REJECTED_MSG: &str = "Client rejected";
28}
29
30pub type DefaultError = serde_json::Value;
31
32#[derive(Debug, Serialize, Deserialize)]
33pub struct ErrorData<E = DefaultError> {
34    pub code: i64,
35    pub message: String,
36    pub data: Option<E>,
37}
38
39impl<E> ErrorData<E> {
40    pub fn into_response<O>(self, id: JsonRpcId) -> JsonRpcResponseFailure<E> {
41        JsonRpcResponseFailure {
42            id,
43            jsonrpc: String::from("2.0"),
44            error: self,
45        }
46    }
47}
48
49impl ErrorData<DefaultError> {
50    pub fn parse_error(_message: String) -> Self {
51        Self {
52            code: codes::PARSE_ERROR_CODE,
53            message: String::from(codes::PARSE_ERROR_MSG),
54            data: None,
55        }
56    }
57
58    pub fn invalid_request(_message: String) -> Self {
59        Self {
60            code: codes::INVALID_REQUEST_CODE,
61            message: codes::INVALID_REQUEST_MSG.into(),
62            data: None,
63        }
64    }
65
66    pub fn method_not_found(method: &str) -> Self {
67        Self {
68            code: codes::METHOD_NOT_FOUND_CODE,
69            message: codes::METHOD_NOT_FOUND_MSG.into(),
70            data: Some(serde_json::json!({"method" : method})),
71        }
72    }
73
74    pub fn not_found() -> Self {
75        Self {
76            code: 404,
77            message: String::from("Not Found"),
78            data: None,
79        }
80    }
81}
82
83impl<E> ErrorData<E> {
84    pub fn invalid_params(data: E) -> Self {
85        Self {
86            code: codes::INVALID_PARAMS_CODE,
87            message: codes::INVALID_PARAMS_MSG.into(),
88            data: Some(data),
89        }
90    }
91}
92
93impl<E> ErrorData<E> {
94    pub fn internal_error(data: E) -> Self {
95        Self {
96            code: codes::INTERNAL_ERROR_CODE,
97            message: codes::INTERNAL_ERROR_MSG.into(),
98            data: Some(data),
99        }
100    }
101}
102
103impl ErrorData<DefaultError> {
104    pub fn internalize<T: core::fmt::Debug>(err: T) -> Self {
105        Self::internal_error(serde_json::Value::String(format!("{:?}", err)))
106    }
107}