bsp_server/
error.rs

1use crate::Notification;
2use crate::Request;
3use std::fmt;
4
5#[derive(Debug, Clone)]
6pub struct ProtocolError(pub(crate) String);
7
8impl fmt::Display for ProtocolError {
9    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10        fmt::Display::fmt(&self.0, f)
11    }
12}
13
14impl std::error::Error for ProtocolError {}
15
16impl From<anyhow::Error> for ProtocolError {
17    fn from(err: anyhow::Error) -> Self {
18        Self(err.to_string())
19    }
20}
21
22#[derive(Debug)]
23pub enum ExtractError<T> {
24    /// The extracted message was of a different method than expected.
25    MethodMismatch(T),
26    /// Failed to deserialize the message.
27    JsonError {
28        method: String,
29        error: serde_json::Error,
30    },
31}
32
33impl std::error::Error for ExtractError<Request> {}
34impl fmt::Display for ExtractError<Request> {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        match self {
37            ExtractError::MethodMismatch(req) => {
38                write!(f, "Method mismatch for request '{}'", req.method())
39            }
40            ExtractError::JsonError { method, error } => {
41                write!(f, "Invalid request\nMethod: {method}\n error: {error}",)
42            }
43        }
44    }
45}
46
47impl std::error::Error for ExtractError<Notification> {}
48impl fmt::Display for ExtractError<Notification> {
49    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50        match self {
51            ExtractError::MethodMismatch(req) => {
52                write!(f, "Method mismatch for notification '{}'", req.method())
53            }
54            ExtractError::JsonError { method, error } => {
55                write!(f, "Invalid notification\nMethod: {method}\n error: {error}")
56            }
57        }
58    }
59}
60
61#[derive(Clone, Copy, Debug)]
62#[allow(unused)]
63pub enum ErrorCode {
64    ParseError = -32700,
65    InvalidRequest = -32600,
66    MethodNotFound = -32601,
67    InvalidParams = -32602,
68    InternalError = -32603,
69    ServerErrorStart = -32099,
70    ServerErrorEnd = -32000,
71    ServerNotInitialized = -32002,
72    RequestCanceled = -32800,
73    ContentModified = -32801,
74    ServerCancelled = -32802,
75}