1 use std::io; 2 use std::string::FromUtf8Error; 3 use thiserror::Error; 4 5 pub type Result<T> = std::result::Result<T, Error>; 6 7 #[derive(Debug, Error, PartialEq)] 8 #[non_exhaustive] 9 pub enum Error { 10 #[error( 11 "DataChannel message is not long enough to determine type: (expected: {expected}, actual: {actual})" 12 )] 13 UnexpectedEndOfBuffer { expected: usize, actual: usize }, 14 #[error("Unknown MessageType {0}")] 15 InvalidMessageType(u8), 16 #[error("Unknown ChannelType {0}")] 17 InvalidChannelType(u8), 18 #[error("Unknown PayloadProtocolIdentifier {0}")] 19 InvalidPayloadProtocolIdentifier(u8), 20 #[error("Stream closed")] 21 ErrStreamClosed, 22 23 #[error("{0}")] 24 Util(#[from] util::Error), 25 #[error("{0}")] 26 Sctp(#[from] sctp::Error), 27 #[error("utf-8 error: {0}")] 28 Utf8(#[from] FromUtf8Error), 29 30 #[allow(non_camel_case_types)] 31 #[error("{0}")] 32 new(String), 33 } 34 35 impl From<Error> for util::Error { 36 fn from(e: Error) -> Self { 37 util::Error::from_std(e) 38 } 39 } 40 41 impl From<Error> for io::Error { 42 fn from(error: Error) -> Self { 43 match error { 44 e @ Error::Sctp(sctp::Error::ErrEof) => { 45 io::Error::new(io::ErrorKind::UnexpectedEof, e.to_string()) 46 } 47 e @ Error::ErrStreamClosed => { 48 io::Error::new(io::ErrorKind::ConnectionAborted, e.to_string()) 49 } 50 e => io::Error::new(io::ErrorKind::Other, e.to_string()), 51 } 52 } 53 } 54 55 impl PartialEq<util::Error> for Error { 56 fn eq(&self, other: &util::Error) -> bool { 57 if let Some(down) = other.downcast_ref::<Error>() { 58 return self == down; 59 } 60 false 61 } 62 } 63 64 impl PartialEq<Error> for util::Error { 65 fn eq(&self, other: &Error) -> bool { 66 if let Some(down) = self.downcast_ref::<Error>() { 67 return other == down; 68 } 69 false 70 } 71 } 72