sync_ls/
error.rs

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