rymder/
errors.rs

1pub type Result<T> = std::result::Result<T, Error>;
2
3#[derive(Debug)]
4pub enum Error {
5    HealthPingConnectionFailure(String),
6    TimedOut(tokio::time::error::Elapsed),
7    InvalidUri(http::uri::InvalidUri),
8    Rpc(tonic::Status),
9    Transport(tonic::transport::Error),
10    ParseInteger(std::num::ParseIntError),
11    UnknownState(String),
12    InvalidIp {
13        ip_str: String,
14        err: std::net::AddrParseError,
15    },
16    InvalidComponentRange(time::error::ComponentRange),
17}
18
19impl std::error::Error for Error {
20    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
21        Some(match self {
22            Self::HealthPingConnectionFailure(_) | Self::UnknownState(_) => return None,
23            Self::TimedOut(elapsed) => elapsed,
24            Self::InvalidUri(invalid) => invalid,
25            Self::Rpc(status) => status,
26            Self::Transport(transport) => transport,
27            Self::ParseInteger(parse) => parse,
28            Self::InvalidIp { err, .. } => err,
29            Self::InvalidComponentRange(_err) => return None,
30        })
31    }
32}
33
34use std::fmt;
35
36impl fmt::Display for Error {
37    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38        match self {
39            Self::HealthPingConnectionFailure(inner) => {
40                write!(f, "health ping connection failure: `{}`", inner)
41            }
42            Self::TimedOut(elapsed) => write!(f, "{}", elapsed),
43            Self::InvalidUri(invalid) => write!(f, "failed to parse connection uri: `{}`", invalid),
44            Self::Rpc(status) => write!(f, "rpc failure: `{}`", status),
45            Self::Transport(transport) => write!(f, "transport failure: `{}`", transport),
46            Self::ParseInteger(parse) => write!(f, "failed to parse integer: `{}`", parse),
47            Self::UnknownState(us) => write!(f, "received an unknown state '{}' from agones", us),
48            Self::InvalidIp { ip_str, err } => {
49                write!(f, "failed to parse ip '{}': {}", ip_str, err)
50            }
51            Self::InvalidComponentRange(cr) => {
52                write!(f, "invalid component range: {}", cr)
53            }
54        }
55    }
56}
57
58impl From<tokio::time::error::Elapsed> for Error {
59    #[inline]
60    fn from(e: tokio::time::error::Elapsed) -> Self {
61        Self::TimedOut(e)
62    }
63}
64
65impl From<http::uri::InvalidUri> for Error {
66    #[inline]
67    fn from(e: http::uri::InvalidUri) -> Self {
68        Self::InvalidUri(e)
69    }
70}
71
72impl From<tonic::Status> for Error {
73    #[inline]
74    fn from(e: tonic::Status) -> Self {
75        Self::Rpc(e)
76    }
77}
78
79impl From<tonic::transport::Error> for Error {
80    #[inline]
81    fn from(e: tonic::transport::Error) -> Self {
82        Self::Transport(e)
83    }
84}
85
86impl From<time::error::ComponentRange> for Error {
87    #[inline]
88    fn from(e: time::error::ComponentRange) -> Self {
89        Self::InvalidComponentRange(e)
90    }
91}