tower_lsp/jsonrpc/
error.rs
use std::fmt::{self, Display, Formatter};
use serde::de::Deserializer;
use serde::ser::Serializer;
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ErrorCode {
ParseError,
InvalidRequest,
MethodNotFound,
InvalidParams,
InternalError,
ServerError(i64),
RequestCancelled,
ContentModified,
}
impl ErrorCode {
#[inline]
pub fn code(&self) -> i64 {
match *self {
ErrorCode::ParseError => -32700,
ErrorCode::InvalidRequest => -32600,
ErrorCode::MethodNotFound => -32601,
ErrorCode::InvalidParams => -32602,
ErrorCode::InternalError => -32603,
ErrorCode::RequestCancelled => -32800,
ErrorCode::ContentModified => -32801,
ErrorCode::ServerError(code) => code,
}
}
#[inline]
pub fn description(&self) -> &'static str {
match *self {
ErrorCode::ParseError => "Parse error",
ErrorCode::InvalidRequest => "Invalid request",
ErrorCode::MethodNotFound => "Method not found",
ErrorCode::InvalidParams => "Invalid params",
ErrorCode::InternalError => "Internal error",
ErrorCode::RequestCancelled => "Canceled",
ErrorCode::ContentModified => "Content modified",
ErrorCode::ServerError(_) => "Server error",
}
}
}
impl From<i64> for ErrorCode {
#[inline]
fn from(code: i64) -> Self {
match code {
-32700 => ErrorCode::ParseError,
-32600 => ErrorCode::InvalidRequest,
-32601 => ErrorCode::MethodNotFound,
-32602 => ErrorCode::InvalidParams,
-32603 => ErrorCode::InternalError,
-32800 => ErrorCode::RequestCancelled,
-32801 => ErrorCode::ContentModified,
code => ErrorCode::ServerError(code),
}
}
}
impl Display for ErrorCode {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Display::fmt(&self.code(), f)
}
}
impl<'a> Deserialize<'a> for ErrorCode {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: Deserializer<'a>,
{
let code: i64 = Deserialize::deserialize(deserializer)?;
Ok(ErrorCode::from(code))
}
}
impl Serialize for ErrorCode {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
self.code().serialize(serializer)
}
}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Error {
pub code: ErrorCode,
pub message: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<Value>,
}
impl Error {
#[inline]
pub fn new(code: ErrorCode) -> Self {
Error {
code,
message: code.description().to_string(),
data: None,
}
}
#[inline]
pub fn parse_error() -> Self {
Error::new(ErrorCode::ParseError)
}
#[inline]
pub fn invalid_request() -> Self {
Error::new(ErrorCode::InvalidRequest)
}
#[inline]
pub fn method_not_found() -> Self {
Error::new(ErrorCode::MethodNotFound)
}
#[inline]
pub fn invalid_params<M>(message: M) -> Self
where
M: Into<String>,
{
Error {
code: ErrorCode::InvalidParams,
message: message.into(),
data: None,
}
}
#[inline]
pub fn internal_error() -> Self {
Error::new(ErrorCode::InternalError)
}
#[inline]
pub fn request_cancelled() -> Self {
Error::new(ErrorCode::RequestCancelled)
}
#[inline]
pub fn content_modified() -> Self {
Error::new(ErrorCode::ContentModified)
}
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}: {}", self.code.description(), self.message)
}
}
impl std::error::Error for Error {}