gl_client/lsps/
error.rs

1use thiserror::Error;
2
3#[derive(Error, Debug)]
4pub enum LspsError {
5    #[error("Unknown method")]
6    MethodUnknown(String),
7    #[error("Failed to parse json-request")]
8    JsonParseRequestError(serde_json::Error),
9    #[error("Failed to parse json-response")]
10    JsonParseResponseError(serde_json::Error),
11    #[error("Error while calling lightning grpc-method")]
12    GrpcError(#[from] tonic::Status),
13    #[error("Connection closed")]
14    ConnectionClosed,
15    #[error("Timeout")]
16    Timeout,
17    #[error("Something unexpected happened")]
18    Other(String),
19}
20
21impl From<std::io::Error> for LspsError {
22    fn from(value: std::io::Error) -> Self {
23        Self::Other(value.to_string())
24    }
25}
26
27pub fn map_json_rpc_error_code_to_str(code: i64) -> &'static str {
28    match code {
29        -32700 => "parsing_error",
30        -32600 => "invalid_request",
31        -32601 => "method_not_found",
32        -32602 => "invalid_params",
33        -32603 => "internal_error",
34        -32099..=-32000 => "implementation_defined_server_error",
35        _ => "unknown_error_code",
36    }
37}
38
39#[cfg(test)]
40mod test {
41    use crate::lsps::error::map_json_rpc_error_code_to_str;
42
43    #[test]
44    fn test_map_json_rpc_error_code_to_str() {
45        assert_eq!(map_json_rpc_error_code_to_str(12), "unknown_error_code");
46        assert_eq!(map_json_rpc_error_code_to_str(-32603), "internal_error");
47    }
48}