use crate::Notification;
use crate::Request;
use std::fmt;
#[derive(Debug, Clone)]
pub struct ProtocolError(pub(crate) String);
impl fmt::Display for ProtocolError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
impl std::error::Error for ProtocolError {}
impl From<anyhow::Error> for ProtocolError {
fn from(err: anyhow::Error) -> Self {
Self(err.to_string())
}
}
#[derive(Debug)]
pub enum ExtractError<T> {
MethodMismatch(T),
JsonError {
method: String,
error: serde_json::Error,
},
}
impl std::error::Error for ExtractError<Request> {}
impl fmt::Display for ExtractError<Request> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ExtractError::MethodMismatch(req) => {
write!(f, "Method mismatch for request '{}'", req.method())
}
ExtractError::JsonError { method, error } => {
write!(f, "Invalid request\nMethod: {method}\n error: {error}",)
}
}
}
}
impl std::error::Error for ExtractError<Notification> {}
impl fmt::Display for ExtractError<Notification> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ExtractError::MethodMismatch(req) => {
write!(f, "Method mismatch for notification '{}'", req.method())
}
ExtractError::JsonError { method, error } => {
write!(f, "Invalid notification\nMethod: {method}\n error: {error}")
}
}
}
}
#[derive(Clone, Copy, Debug)]
#[allow(unused)]
pub enum ErrorCode {
ParseError = -32700,
InvalidRequest = -32600,
MethodNotFound = -32601,
InvalidParams = -32602,
InternalError = -32603,
ServerErrorStart = -32099,
ServerErrorEnd = -32000,
ServerNotInitialized = -32002,
RequestCanceled = -32800,
ContentModified = -32801,
ServerCancelled = -32802,
}