Skip to main content

str0m/sctp/
error.rs

1use sctp_proto::Error as ProtoError;
2use std::error::Error;
3use std::fmt;
4
5/// Errors from the SCTP subsystem.
6#[derive(Debug, Eq, Clone, PartialEq)]
7pub enum SctpError {
8    /// Some protocol error as wrapped from the sctp_proto crate.
9    Proto(ProtoError),
10
11    /// Stream was not ready and we tried to write.
12    WriteBeforeEstablished,
13
14    /// The initial DCEP is not valid.
15    DcepOpenTooSmall,
16
17    /// The initial DCEP is not the correct message type.
18    DcepIncorrectMessageType,
19
20    /// The initial DCEP cant be read as utf-8.
21    DcepBadUtf8,
22
23    /// Invalid SNAP token in SCTP INIT string.
24    InvalidSnap,
25}
26
27impl fmt::Display for SctpError {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        match self {
30            SctpError::Proto(err) => write!(f, "{}", err),
31            SctpError::WriteBeforeEstablished => {
32                write!(f, "Write on a stream before it was established")
33            }
34            SctpError::DcepOpenTooSmall => write!(f, "DCEP open message too small"),
35            SctpError::DcepIncorrectMessageType => write!(f, "DCEP incorrect message type"),
36            SctpError::DcepBadUtf8 => write!(f, "DCEP bad UTF-8 string"),
37            SctpError::InvalidSnap => write!(f, "Invalid SNAP token"),
38        }
39    }
40}
41
42impl Error for SctpError {
43    fn source(&self) -> Option<&(dyn Error + 'static)> {
44        match self {
45            SctpError::Proto(err) => Some(err),
46            _ => None,
47        }
48    }
49}
50
51impl From<ProtoError> for SctpError {
52    fn from(err: ProtoError) -> Self {
53        SctpError::Proto(err)
54    }
55}