xref: /xiu/protocol/rtsp/src/session/errors.rs (revision cbe12ea9)
1 use {
2     crate::rtp::errors::{PackerError, UnPackerError},
3     bytesio::bytes_errors::BytesReadError,
4     bytesio::{bytes_errors::BytesWriteError, bytesio_errors::BytesIOError},
5     failure::{Backtrace, Fail},
6     std::fmt,
7     std::str::Utf8Error,
8 };
9 
10 #[derive(Debug)]
11 pub struct SessionError {
12     pub value: SessionErrorValue,
13 }
14 
15 #[derive(Debug, Fail)]
16 pub enum SessionErrorValue {
17     #[fail(display = "net io error: {}\n", _0)]
18     BytesIOError(#[cause] BytesIOError),
19     #[fail(display = "bytes read error: {}\n", _0)]
20     BytesReadError(#[cause] BytesReadError),
21     #[fail(display = "bytes write error: {}\n", _0)]
22     BytesWriteError(#[cause] BytesWriteError),
23     #[fail(display = "Utf8Error: {}\n", _0)]
24     Utf8Error(#[cause] Utf8Error),
25     #[fail(display = "UnPackerError: {}\n", _0)]
26     UnPackerError(#[cause] UnPackerError),
27     #[fail(display = "stream hub event send error\n")]
28     StreamHubEventSendErr,
29     #[fail(display = "cannot receive frame data from stream hub\n")]
30     CannotReceiveFrameData,
31     #[fail(display = "pack error: {}\n", _0)]
32     PackerError(#[cause] PackerError),
33 }
34 
35 impl From<BytesIOError> for SessionError {
36     fn from(error: BytesIOError) -> Self {
37         SessionError {
38             value: SessionErrorValue::BytesIOError(error),
39         }
40     }
41 }
42 
43 impl From<BytesReadError> for SessionError {
44     fn from(error: BytesReadError) -> Self {
45         SessionError {
46             value: SessionErrorValue::BytesReadError(error),
47         }
48     }
49 }
50 
51 impl From<BytesWriteError> for SessionError {
52     fn from(error: BytesWriteError) -> Self {
53         SessionError {
54             value: SessionErrorValue::BytesWriteError(error),
55         }
56     }
57 }
58 
59 impl From<Utf8Error> for SessionError {
60     fn from(error: Utf8Error) -> Self {
61         SessionError {
62             value: SessionErrorValue::Utf8Error(error),
63         }
64     }
65 }
66 
67 impl From<PackerError> for SessionError {
68     fn from(error: PackerError) -> Self {
69         SessionError {
70             value: SessionErrorValue::PackerError(error),
71         }
72     }
73 }
74 
75 impl From<UnPackerError> for SessionError {
76     fn from(error: UnPackerError) -> Self {
77         SessionError {
78             value: SessionErrorValue::UnPackerError(error),
79         }
80     }
81 }
82 
83 impl fmt::Display for SessionError {
84     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
85         fmt::Display::fmt(&self.value, f)
86     }
87 }
88 
89 impl Fail for SessionError {
90     fn cause(&self) -> Option<&dyn Fail> {
91         self.value.cause()
92     }
93 
94     fn backtrace(&self) -> Option<&Backtrace> {
95         self.value.backtrace()
96     }
97 }
98