wamp_core/
error.rs

1use crate::messages::{
2    Abort, Authenticate, Call, Cancel, Challenge, Event, Goodbye, Hello, Interrupt, Invocation,
3    Messages, Publish, Published, Register, Registered, Subscribe, Subscribed, Unregister,
4    Unregistered, Unsubscribe, Unsubscribed, WampError, WampResult, Welcome, Yield,
5};
6use tungstenite::http::header::{InvalidHeaderValue, ToStrError};
7
8#[derive(Debug)]
9pub enum Error {
10    InvalidURI,
11    ToStrError(ToStrError),
12    InvalidHeaderValue(InvalidHeaderValue),
13    TungsteniteError(tungstenite::Error),
14    SerdeJsonError(serde_json::Error),
15    InvalidMessageEnumMember,
16    Error(&'static str),
17    InvalidFrameReceived(Messages),
18    Close,
19    Abort(Abort),
20    NoSuchWampErrorType(Messages),
21    NoSuchMessage,
22}
23
24macro_rules! message_to_from {
25    ($typ: ident) => {
26        impl TryFrom<$typ> for tungstenite::Message {
27            type Error = serde_json::Error;
28
29            fn try_from(value: $typ) -> Result<tungstenite::Message, Self::Error> {
30                Ok(tungstenite::Message::Text(serde_json::to_string(&value)?))
31            }
32        }
33    };
34}
35
36//message_to_from!(Abort);
37message_to_from!(Abort);
38message_to_from!(Authenticate);
39message_to_from!(Call);
40message_to_from!(Cancel);
41message_to_from!(Challenge);
42message_to_from!(WampError);
43message_to_from!(WampResult);
44message_to_from!(Event);
45message_to_from!(Goodbye);
46message_to_from!(Hello);
47message_to_from!(Interrupt);
48message_to_from!(Invocation);
49message_to_from!(Publish);
50message_to_from!(Published);
51message_to_from!(Register);
52message_to_from!(Registered);
53message_to_from!(Subscribe);
54message_to_from!(Subscribed);
55message_to_from!(Unregister);
56message_to_from!(Unregistered);
57message_to_from!(Unsubscribe);
58message_to_from!(Unsubscribed);
59message_to_from!(Welcome);
60message_to_from!(Yield);
61
62//impl<M: WampMessage + Serialize> TryFrom<M> for crate::error::Error {
63//    type Error = Error;
64//
65//    fn try_from(value: M) -> Result<Self, Self::Error> {
66//
67//    }
68//}
69
70impl From<serde_json::Error> for Error {
71    fn from(value: serde_json::Error) -> Self {
72        Self::SerdeJsonError(value)
73    }
74}
75
76impl From<tungstenite::Error> for Error {
77    fn from(value: tungstenite::Error) -> Self {
78        Self::TungsteniteError(value)
79    }
80}
81
82#[derive(Debug)]
83/// # [TODO]: WampErrorUri
84/// Unimplemented, unfortunately this does absolutely nothing in the current moment. The reasons are described below.
85/// 
86/// ## The Problem
87/// Wamp URI's have a variable amount of error URIs that get sent with different enabled features on wamp routers.
88/// This leads to the possibility of also running into "unknown errors". This is running with the assumption that we add
89/// in each string manually to serde to parse the error to the enum variant.
90/// 
91/// Which, also isnt how the wamp protocol defines how to parse URIs. While I understand from the documents that URIs are
92/// parsed using Regex, I have gotten extremely inconsistent results while testing with errors using Regex to parse URIs.
93/// 
94/// To further explain, while there is some level of structure to the Regex they use in reference to what type of URI it takes,
95/// and I have modeled that into a rust like structure, using the Regex on actual URI's from the wamp protocol returns very mixed 
96/// (and almost always wrong on edge cases) results.
97/// 
98/// I will stop documenting here to cite myself, more investigation is needed.
99pub enum WampErrorUri {
100    NotAuthorized,
101    ProcedureAlreadyExists,
102    NoSuchRealm,
103    ProtocolViolation,
104    NoSuchSubscription,
105    NoSuchRegistration,
106    InvalidUri,
107    NoSuchProcedure,
108    InvalidArgument,
109    Canceled,
110    PayloadSizeExceeded,
111    FeatureNotSupported,
112    Timeout,
113    Unavailable,
114    NoAvailableCallee,
115    DiscloseMeNotAllowed,
116    OptionDisallowedDiscloseMe,
117    NoMatchingAuthMethod,
118    NoSuchRole,
119    NoSuchPrincipal,
120    AuthenticationDenied,
121    AuthenticationFailed,
122    AuthenticationRequired,
123    AuthorizationDenied,
124    AuthorizationFailed,
125    AuthorizationRequired,
126    NetworkFailure,
127    OptionNotAllowed,
128}
129/// [TODO]: See WampErrorUri Structure for more details.
130pub enum CloseUri {
131    SystemShutdown,
132    CloseRealm,
133    GoodbyeAndOut,
134    Killed,
135}