xref: /xiu/protocol/rtmp/src/session/errors.rs (revision 2b020b03)
1 use {
2     crate::{
3         amf0::errors::Amf0WriteError,
4         chunk::errors::{PackError, UnpackError},
5         handshake::errors::HandshakeError,
6         messages::errors::MessageError,
7         netconnection::errors::NetConnectionError,
8         netstream::errors::NetStreamError,
9         protocol_control_messages::errors::ControlMessagesError,
10         user_control_messages::errors::EventMessagesError,
11     },
12     failure::{Backtrace, Fail},
13     networkio::{bytes_errors::BytesWriteError, networkio_errors::NetIOError},
14     std::fmt,
15 };
16 
17 #[derive(Debug)]
18 pub struct SessionError {
19     pub value: SessionErrorValue,
20 }
21 
22 #[derive(Debug, Fail)]
23 pub enum SessionErrorValue {
24     #[fail(display = "amf0 write error: {}\n", _0)]
25     Amf0WriteError(#[cause] Amf0WriteError),
26     #[fail(display = "bytes write error: {}\n", _0)]
27     BytesWriteError(#[cause] BytesWriteError),
28     // #[fail(display = "timeout error: {}\n", _0)]
29     // TimeoutError(#[cause] Elapsed),
30     #[fail(display = "unpack error: {}\n", _0)]
31     UnPackError(#[cause] UnpackError),
32 
33     #[fail(display = "message error: {}\n", _0)]
34     MessageError(#[cause] MessageError),
35     #[fail(display = "control message error: {}\n", _0)]
36     ControlMessagesError(#[cause] ControlMessagesError),
37     #[fail(display = "net connection error: {}\n", _0)]
38     NetConnectionError(#[cause] NetConnectionError),
39     #[fail(display = "net stream error: {}\n", _0)]
40     NetStreamError(#[cause] NetStreamError),
41 
42     #[fail(display = "event messages error: {}\n", _0)]
43     EventMessagesError(#[cause] EventMessagesError),
44     #[fail(display = "net io error: {}\n", _0)]
45     NetIOError(#[cause] NetIOError),
46     #[fail(display = "pack error: {}\n", _0)]
47     PackError(#[cause] PackError),
48     #[fail(display = "handshake error: {}\n", _0)]
49     HandshakeError(#[cause] HandshakeError),
50 
51     #[fail(display = "amf0 count not correct error\n")]
52     Amf0ValueCountNotCorrect,
53     #[fail(display = "amf0 value type not correct error\n")]
54     Amf0ValueTypeNotCorrect,
55     #[fail(display = "channel event send error\n")]
56     ChannelEventSendErr,
57     #[fail(display = "none channel data sender error\n")]
58     NoneChannelDataSender,
59     #[fail(display = "none channel data receiver error\n")]
60     NoneChannelDataReceiver,
61     #[fail(display = "send channel data error\n")]
62     SendChannelDataErr,
63     #[fail(display = "subscribe count limit is reached.\n")]
64     SubscribeCountLimitReach,
65 
66     #[fail(display = "no app name error\n")]
67     NoAppName,
68 
69     #[fail(display = "session is finished.")]
70     Finish,
71 }
72 
73 impl From<Amf0WriteError> for SessionError {
74     fn from(error: Amf0WriteError) -> Self {
75         SessionError {
76             value: SessionErrorValue::Amf0WriteError(error),
77         }
78     }
79 }
80 
81 impl From<BytesWriteError> for SessionError {
82     fn from(error: BytesWriteError) -> Self {
83         SessionError {
84             value: SessionErrorValue::BytesWriteError(error),
85         }
86     }
87 }
88 
89 // impl From<Elapsed> for SessionError {
90 //     fn from(error: Elapsed) -> Self {
91 //         SessionError {
92 //             value: SessionErrorValue::TimeoutError(error),
93 //         }
94 //     }
95 // }
96 
97 impl From<UnpackError> for SessionError {
98     fn from(error: UnpackError) -> Self {
99         SessionError {
100             value: SessionErrorValue::UnPackError(error),
101         }
102     }
103 }
104 
105 impl From<MessageError> for SessionError {
106     fn from(error: MessageError) -> Self {
107         SessionError {
108             value: SessionErrorValue::MessageError(error),
109         }
110     }
111 }
112 
113 impl From<ControlMessagesError> for SessionError {
114     fn from(error: ControlMessagesError) -> Self {
115         SessionError {
116             value: SessionErrorValue::ControlMessagesError(error),
117         }
118     }
119 }
120 
121 impl From<NetConnectionError> for SessionError {
122     fn from(error: NetConnectionError) -> Self {
123         SessionError {
124             value: SessionErrorValue::NetConnectionError(error),
125         }
126     }
127 }
128 
129 impl From<NetStreamError> for SessionError {
130     fn from(error: NetStreamError) -> Self {
131         SessionError {
132             value: SessionErrorValue::NetStreamError(error),
133         }
134     }
135 }
136 
137 impl From<EventMessagesError> for SessionError {
138     fn from(error: EventMessagesError) -> Self {
139         SessionError {
140             value: SessionErrorValue::EventMessagesError(error),
141         }
142     }
143 }
144 
145 impl From<NetIOError> for SessionError {
146     fn from(error: NetIOError) -> Self {
147         SessionError {
148             value: SessionErrorValue::NetIOError(error),
149         }
150     }
151 }
152 
153 impl From<PackError> for SessionError {
154     fn from(error: PackError) -> Self {
155         SessionError {
156             value: SessionErrorValue::PackError(error),
157         }
158     }
159 }
160 
161 impl From<HandshakeError> for SessionError {
162     fn from(error: HandshakeError) -> Self {
163         SessionError {
164             value: SessionErrorValue::HandshakeError(error),
165         }
166     }
167 }
168 
169 impl fmt::Display for SessionError {
170     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
171         fmt::Display::fmt(&self.value, f)
172     }
173 }
174 
175 impl Fail for SessionError {
176     fn cause(&self) -> Option<&dyn Fail> {
177         self.value.cause()
178     }
179 
180     fn backtrace(&self) -> Option<&Backtrace> {
181         self.value.backtrace()
182     }
183 }
184