1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use std;
use std::io;
use std::sync::mpsc::RecvError;
use trackable::error::{self, IntoTrackableError, TrackableError, ErrorKindExt};
use fibers::sync::oneshot::MonitorError;

use rfc5389::attributes::ErrorCode;
use rfc5389::errors;

/// The error type for this crate.
pub type Error = TrackableError<ErrorKind>;

/// A list of error kind.
#[derive(Debug, Clone)]
pub enum ErrorKind {
    /// The operation timed out.
    Timeout,

    /// The target resource is full (maybe temporary).
    Full,

    /// The input bytes are not a STUN message.
    NotStun(Vec<u8>),

    /// The input is invalid.
    Invalid,

    /// The input is valid, but requires unsupported features by this agent.
    Unsupported,

    /// An error specified by the `ErrorCode` instance.
    ErrorCode(ErrorCode),

    /// Other errors.
    Other,
}
impl error::ErrorKind for ErrorKind {
    fn description(&self) -> &str {
        match *self {
            ErrorKind::Timeout => "The operation timed out",
            ErrorKind::Full => "The target resource is full (maybe temporary)",
            ErrorKind::NotStun(_) => "The input bytes are not a STUN message",
            ErrorKind::Invalid => "The input is invalid",
            ErrorKind::Unsupported => {
                "The input is valid, but requires unsupported features by this agent."
            }
            ErrorKind::ErrorCode(ref e) => e.reason_phrase(),
            ErrorKind::Other => "Some error happened",
        }
    }
}
impl From<ErrorKind> for ErrorCode {
    fn from(f: ErrorKind) -> Self {
        match f {
            ErrorKind::Timeout => ErrorCode::new(408, "Request Timeout".to_string()).unwrap(),
            ErrorKind::Full => ErrorCode::new(503, "Service Unavailable".to_string()).unwrap(),
            ErrorKind::NotStun(_) => errors::BadRequest.into(),
            ErrorKind::Invalid => errors::BadRequest.into(),
            ErrorKind::Unsupported => ErrorCode::new(501, "Not Implemented".to_string()).unwrap(),
            ErrorKind::ErrorCode(code) => code,
            ErrorKind::Other => errors::ServerError.into(),
        }
    }
}
impl IntoTrackableError<MonitorError<Error>> for ErrorKind {
    fn into_trackable_error(f: MonitorError<Error>) -> Error {
        f.unwrap_or(ErrorKind::Other.into())
    }
}
impl IntoTrackableError<io::Error> for ErrorKind {
    fn into_trackable_error(f: io::Error) -> Error {
        ErrorKind::Other.cause(f)
    }
}
impl IntoTrackableError<RecvError> for ErrorKind {
    fn into_trackable_error(f: RecvError) -> Error {
        ErrorKind::Other.cause(f)
    }
}
impl IntoTrackableError<std::time::SystemTimeError> for ErrorKind {
    fn into_trackable_error(f: std::time::SystemTimeError) -> Error {
        ErrorKind::Other.cause(f)
    }
}