1use std::fmt;
2
3use crate::{Notification, Request};
4
5#[derive(Debug, Clone, PartialEq)]
6pub struct ProtocolError(String, bool);
7
8impl ProtocolError {
9 pub(crate) fn new(msg: impl Into<String>) -> Self {
10 ProtocolError(msg.into(), false)
11 }
12
13 pub(crate) fn disconnected() -> ProtocolError {
14 ProtocolError("disconnected channel".into(), true)
15 }
16
17 /// Whether this error occured due to a disconnected channel.
18 pub fn channel_is_disconnected(&self) -> bool {
19 self.1
20 }
21}
22
23impl std::error::Error for ProtocolError {}
24
25impl fmt::Display for ProtocolError {
26 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27 fmt::Display::fmt(&self.0, f)
28 }
29}
30
31#[derive(Debug)]
32pub enum ExtractError<T> {
33 /// The extracted message was of a different method than expected.
34 MethodMismatch(T),
35 /// Failed to deserialize the message.
36 JsonError { method: String, error: serde_json::Error },
37}
38
39impl std::error::Error for ExtractError<Request> {}
40impl fmt::Display for ExtractError<Request> {
41 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42 match self {
43 ExtractError::MethodMismatch(req: &Request) => {
44 write!(f, "Method mismatch for request '{}'", req.method)
45 }
46 ExtractError::JsonError { method: &String, error: &Error } => {
47 write!(f, "Invalid request\nMethod: {method}\n error: {error}",)
48 }
49 }
50 }
51}
52
53impl std::error::Error for ExtractError<Notification> {}
54impl fmt::Display for ExtractError<Notification> {
55 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56 match self {
57 ExtractError::MethodMismatch(req: &Notification) => {
58 write!(f, "Method mismatch for notification '{}'", req.method)
59 }
60 ExtractError::JsonError { method: &String, error: &Error } => {
61 write!(f, "Invalid notification\nMethod: {method}\n error: {error}")
62 }
63 }
64 }
65}
66