xref: /xiu/protocol/rtmp/src/session/errors.rs (revision fcc2ec9e)
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 
64     #[fail(display = "no app name error\n")]
65     NoAppName,
66 
67     #[fail(display = "session is finished.")]
68     Finish,
69 }
70 
71 impl From<Amf0WriteError> for SessionError {
72     fn from(error: Amf0WriteError) -> Self {
73         SessionError {
74             value: SessionErrorValue::Amf0WriteError(error),
75         }
76     }
77 }
78 
79 impl From<BytesWriteError> for SessionError {
80     fn from(error: BytesWriteError) -> Self {
81         SessionError {
82             value: SessionErrorValue::BytesWriteError(error),
83         }
84     }
85 }
86 
87 // impl From<Elapsed> for SessionError {
88 //     fn from(error: Elapsed) -> Self {
89 //         SessionError {
90 //             value: SessionErrorValue::TimeoutError(error),
91 //         }
92 //     }
93 // }
94 
95 impl From<UnpackError> for SessionError {
96     fn from(error: UnpackError) -> Self {
97         SessionError {
98             value: SessionErrorValue::UnPackError(error),
99         }
100     }
101 }
102 
103 impl From<MessageError> for SessionError {
104     fn from(error: MessageError) -> Self {
105         SessionError {
106             value: SessionErrorValue::MessageError(error),
107         }
108     }
109 }
110 
111 impl From<ControlMessagesError> for SessionError {
112     fn from(error: ControlMessagesError) -> Self {
113         SessionError {
114             value: SessionErrorValue::ControlMessagesError(error),
115         }
116     }
117 }
118 
119 impl From<NetConnectionError> for SessionError {
120     fn from(error: NetConnectionError) -> Self {
121         SessionError {
122             value: SessionErrorValue::NetConnectionError(error),
123         }
124     }
125 }
126 
127 impl From<NetStreamError> for SessionError {
128     fn from(error: NetStreamError) -> Self {
129         SessionError {
130             value: SessionErrorValue::NetStreamError(error),
131         }
132     }
133 }
134 
135 impl From<EventMessagesError> for SessionError {
136     fn from(error: EventMessagesError) -> Self {
137         SessionError {
138             value: SessionErrorValue::EventMessagesError(error),
139         }
140     }
141 }
142 
143 impl From<NetIOError> for SessionError {
144     fn from(error: NetIOError) -> Self {
145         SessionError {
146             value: SessionErrorValue::NetIOError(error),
147         }
148     }
149 }
150 
151 impl From<PackError> for SessionError {
152     fn from(error: PackError) -> Self {
153         SessionError {
154             value: SessionErrorValue::PackError(error),
155         }
156     }
157 }
158 
159 impl From<HandshakeError> for SessionError {
160     fn from(error: HandshakeError) -> Self {
161         SessionError {
162             value: SessionErrorValue::HandshakeError(error),
163         }
164     }
165 }
166 
167 impl fmt::Display for SessionError {
168     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
169         fmt::Display::fmt(&self.value, f)
170     }
171 }
172 
173 impl Fail for SessionError {
174     fn cause(&self) -> Option<&dyn Fail> {
175         self.value.cause()
176     }
177 
178     fn backtrace(&self) -> Option<&Backtrace> {
179         self.value.backtrace()
180     }
181 }
182