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