sync_ls/
error.rs

1#![allow(missing_docs)]
2#![allow(unused)]
3
4use std::fmt;
5
6#[cfg(feature = "lsp")]
7use crate::lsp::{Notification, Request};
8
9#[derive(Debug, Clone, PartialEq)]
10pub struct ProtocolError(String, bool);
11
12impl ProtocolError {
13    pub(crate) fn new(msg: impl Into<String>) -> Self {
14        ProtocolError(msg.into(), false)
15    }
16
17    pub(crate) fn disconnected() -> ProtocolError {
18        ProtocolError("disconnected channel".into(), true)
19    }
20
21    /// Whether this error occurred due to a disconnected channel.
22    pub fn channel_is_disconnected(&self) -> bool {
23        self.1
24    }
25}
26
27impl std::error::Error for ProtocolError {}
28
29impl fmt::Display for ProtocolError {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        fmt::Display::fmt(&self.0, f)
32    }
33}
34
35#[derive(Debug)]
36pub enum ExtractError<T> {
37    /// The extracted message was of a different method than expected.
38    MethodMismatch(T),
39    /// Failed to deserialize the message.
40    JsonError {
41        method: String,
42        error: serde_json::Error,
43    },
44}
45
46#[cfg(feature = "lsp")]
47impl std::error::Error for ExtractError<Request> {}
48#[cfg(feature = "lsp")]
49impl fmt::Display for ExtractError<Request> {
50    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51        match self {
52            ExtractError::MethodMismatch(req) => {
53                write!(f, "Method mismatch for request '{}'", req.method)
54            }
55            ExtractError::JsonError { method, error } => {
56                write!(f, "Invalid request\nMethod: {method}\n error: {error}",)
57            }
58        }
59    }
60}
61
62#[cfg(feature = "lsp")]
63impl std::error::Error for ExtractError<Notification> {}
64#[cfg(feature = "lsp")]
65impl fmt::Display for ExtractError<Notification> {
66    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67        match self {
68            ExtractError::MethodMismatch(req) => {
69                write!(f, "Method mismatch for notification '{}'", req.method)
70            }
71            ExtractError::JsonError { method, error } => {
72                write!(f, "Invalid notification\nMethod: {method}\n error: {error}")
73            }
74        }
75    }
76}