xref: /xiu/protocol/webrtc/src/session/errors.rs (revision 80f20d70)
1 use {
2     bytesio::bytes_errors::BytesReadError,
3     bytesio::{bytes_errors::BytesWriteError, bytesio_errors::BytesIOError},
4     failure::{Backtrace, Fail},
5     std::fmt,
6     std::str::Utf8Error,
7     webrtc::error::Error as RTCError,
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 = "webrtc error: {}\n", _0)]
26     RTCError(#[cause] RTCError),
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 = "Http Request path error\n")]
32     HttpRequestPathError,
33     #[fail(display = "Not supported\n")]
34     HttpRequestNotSupported,
35     #[fail(display = "Empty sdp data\n")]
36     HttpRequestEmptySdp,
37     #[fail(display = "Cannot find Content-Length\n")]
38     HttpRequestNoContentLength,
39 }
40 
41 impl From<RTCError> for SessionError {
42     fn from(error: RTCError) -> Self {
43         SessionError {
44             value: SessionErrorValue::RTCError(error),
45         }
46     }
47 }
48 
49 impl From<BytesIOError> for SessionError {
50     fn from(error: BytesIOError) -> Self {
51         SessionError {
52             value: SessionErrorValue::BytesIOError(error),
53         }
54     }
55 }
56 
57 impl From<BytesReadError> for SessionError {
58     fn from(error: BytesReadError) -> Self {
59         SessionError {
60             value: SessionErrorValue::BytesReadError(error),
61         }
62     }
63 }
64 
65 impl From<BytesWriteError> for SessionError {
66     fn from(error: BytesWriteError) -> Self {
67         SessionError {
68             value: SessionErrorValue::BytesWriteError(error),
69         }
70     }
71 }
72 
73 impl From<Utf8Error> for SessionError {
74     fn from(error: Utf8Error) -> Self {
75         SessionError {
76             value: SessionErrorValue::Utf8Error(error),
77         }
78     }
79 }
80 
81 impl fmt::Display for SessionError {
82     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
83         fmt::Display::fmt(&self.value, f)
84     }
85 }
86 
87 impl Fail for SessionError {
88     fn cause(&self) -> Option<&dyn Fail> {
89         self.value.cause()
90     }
91 
92     fn backtrace(&self) -> Option<&Backtrace> {
93         self.value.backtrace()
94     }
95 }
96