use std::{
error::Error,
fmt::{Debug, Display},
};
use lsp_server::{ErrorCode, ExtractError};
pub trait AsLspError: std::error::Error {
fn code(&self) -> ErrorCode {
ErrorCode::UnknownErrorCode
}
}
pub struct LspError(Box<dyn AsLspError>);
impl LspError {
pub fn code(&self) -> ErrorCode {
self.0.code()
}
}
impl Debug for LspError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Debug::fmt(&self.0, f)
}
}
impl Display for LspError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.0, f)
}
}
impl Error for LspError {}
impl<T: AsLspError + 'static> From<T> for LspError {
fn from(value: T) -> Self {
Self(Box::new(value))
}
}
pub type LspResult<T> = std::result::Result<T, LspError>;
impl AsLspError for ExtractError<lsp_server::Request> {
fn code(&self) -> ErrorCode {
match self {
ExtractError::MethodMismatch(_) => ErrorCode::InternalError,
ExtractError::JsonError { .. } => ErrorCode::InvalidParams,
}
}
}
impl AsLspError for serde_json::Error {
fn code(&self) -> ErrorCode {
ErrorCode::InternalError
}
}
pub struct LspErrorString(pub String, pub ErrorCode);
impl AsLspError for LspErrorString {
fn code(&self) -> ErrorCode {
self.1
}
}
impl Error for LspErrorString {}
impl Debug for LspErrorString {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Debug::fmt(&self.0, f)
}
}
impl Display for LspErrorString {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Debug::fmt(&self.0, f)
}
}