• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use std::fmt;
2 
3 use crate::{Notification, Request};
4 
5 #[derive(Debug, Clone, PartialEq)]
6 pub struct ProtocolError(pub(crate) String);
7 
8 impl std::error::Error for ProtocolError {}
9 
10 impl fmt::Display for ProtocolError {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result11     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
12         fmt::Display::fmt(&self.0, f)
13     }
14 }
15 
16 #[derive(Debug)]
17 pub enum ExtractError<T> {
18     /// The extracted message was of a different method than expected.
19     MethodMismatch(T),
20     /// Failed to deserialize the message.
21     JsonError { method: String, error: serde_json::Error },
22 }
23 
24 impl std::error::Error for ExtractError<Request> {}
25 impl fmt::Display for ExtractError<Request> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result26     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27         match self {
28             ExtractError::MethodMismatch(req) => {
29                 write!(f, "Method mismatch for request '{}'", req.method)
30             }
31             ExtractError::JsonError { method, error } => {
32                 write!(f, "Invalid request\nMethod: {method}\n error: {error}",)
33             }
34         }
35     }
36 }
37 
38 impl std::error::Error for ExtractError<Notification> {}
39 impl fmt::Display for ExtractError<Notification> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result40     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41         match self {
42             ExtractError::MethodMismatch(req) => {
43                 write!(f, "Method mismatch for notification '{}'", req.method)
44             }
45             ExtractError::JsonError { method, error } => {
46                 write!(f, "Invalid notification\nMethod: {method}\n error: {error}")
47             }
48         }
49     }
50 }
51