ra_ap_lsp_server/
error.rs

1use std::fmt;
2
3use crate::{Notification, Request};
4
5#[derive(Debug, Clone)]
6pub struct ProtocolError(pub(crate) String);
7
8impl std::error::Error for ProtocolError {}
9
10impl fmt::Display for ProtocolError {
11    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
12        fmt::Display::fmt(&self.0, f)
13    }
14}
15
16#[derive(Debug)]
17pub enum ExtractError<T> {
18    /// The extracted message was of a different method than expected.
19    MethodMismatch(T),
20    /// Failed to deserialize the message.
21    JsonError { method: String, error: serde_json::Error },
22}
23
24impl std::error::Error for ExtractError<Request> {}
25impl fmt::Display for ExtractError<Request> {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        match self {
28            ExtractError::MethodMismatch(req) => {
29                write!(f, "Method mismatch for request '{}'", req.method)
30            }
31            ExtractError::JsonError { method, error } => {
32                write!(f, "Invalid request\nMethod: {method}\n error: {error}",)
33            }
34        }
35    }
36}
37
38impl std::error::Error for ExtractError<Notification> {}
39impl fmt::Display for ExtractError<Notification> {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        match self {
42            ExtractError::MethodMismatch(req) => {
43                write!(f, "Method mismatch for notification '{}'", req.method)
44            }
45            ExtractError::JsonError { method, error } => {
46                write!(f, "Invalid notification\nMethod: {method}\n error: {error}")
47            }
48        }
49    }
50}