Skip to main content

wraith_lsp/
error.rs

1use std::fmt::{Display, Formatter};
2use std::path::PathBuf;
3
4#[derive(Debug)]
5pub enum LspError {
6    Io(std::io::Error),
7    Json(serde_json::Error),
8    InvalidHeader(String),
9    MissingContentLength,
10    InvalidContentLength(String),
11    UnsupportedDocument(PathBuf),
12    UnknownServer(String),
13    DuplicateExtension {
14        extension: String,
15        existing_server: String,
16        new_server: String,
17    },
18    PathToUrl(PathBuf),
19    Protocol(String),
20}
21
22impl Display for LspError {
23    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
24        match self {
25            Self::Io(error) => write!(f, "{error}"),
26            Self::Json(error) => write!(f, "{error}"),
27            Self::InvalidHeader(header) => write!(f, "invalid LSP header: {header}"),
28            Self::MissingContentLength => write!(f, "missing LSP Content-Length header"),
29            Self::InvalidContentLength(value) => {
30                write!(f, "invalid LSP Content-Length value: {value}")
31            }
32            Self::UnsupportedDocument(path) => {
33                write!(f, "no LSP server configured for {}", path.display())
34            }
35            Self::UnknownServer(name) => write!(f, "unknown LSP server: {name}"),
36            Self::DuplicateExtension {
37                extension,
38                existing_server,
39                new_server,
40            } => write!(
41                f,
42                "duplicate LSP extension mapping for {extension}: {existing_server} and {new_server}"
43            ),
44            Self::PathToUrl(path) => write!(f, "failed to convert path to file URL: {}", path.display()),
45            Self::Protocol(message) => write!(f, "LSP protocol error: {message}"),
46        }
47    }
48}
49
50impl std::error::Error for LspError {}
51
52impl From<std::io::Error> for LspError {
53    fn from(value: std::io::Error) -> Self {
54        Self::Io(value)
55    }
56}
57
58impl From<serde_json::Error> for LspError {
59    fn from(value: serde_json::Error) -> Self {
60        Self::Json(value)
61    }
62}