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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use std::convert::Infallible;

use crate::client::ExclusiveBody;

pub enum BadRequestReason {
    MissingHeader(&'static str),
    InvalidHeader(&'static str),
}

pub type FaucetResult<T> = std::result::Result<T, FaucetError>;

pub enum FaucetError {
    PoolBuild(deadpool::managed::BuildError),
    PoolTimeout(deadpool::managed::TimeoutType),
    PoolPostCreateHook,
    PoolClosed,
    PoolNoRuntimeSpecified,
    RecvError(tokio::sync::watch::error::RecvError),
    Io(std::io::Error),
    Unknown(String),
    HostParseError(std::net::AddrParseError),
    Hyper(hyper::Error),
    Infallible(Infallible),
    BadRequest(BadRequestReason),
    InvalidHeaderValues(hyper::header::InvalidHeaderValue),
    Http(hyper::http::Error),
}

impl From<hyper::header::InvalidHeaderValue> for FaucetError {
    fn from(e: hyper::header::InvalidHeaderValue) -> Self {
        Self::InvalidHeaderValues(e)
    }
}

impl From<hyper::http::Error> for FaucetError {
    fn from(e: hyper::http::Error) -> Self {
        Self::Http(e)
    }
}

impl From<deadpool::managed::PoolError<FaucetError>> for FaucetError {
    fn from(value: deadpool::managed::PoolError<FaucetError>) -> Self {
        match value {
            deadpool::managed::PoolError::Backend(e) => e,
            deadpool::managed::PoolError::Timeout(e) => Self::PoolTimeout(e),
            deadpool::managed::PoolError::Closed => Self::PoolClosed,
            deadpool::managed::PoolError::PostCreateHook(_) => Self::PoolPostCreateHook,
            deadpool::managed::PoolError::NoRuntimeSpecified => Self::PoolNoRuntimeSpecified,
        }
    }
}

impl From<tokio::sync::watch::error::RecvError> for FaucetError {
    fn from(e: tokio::sync::watch::error::RecvError) -> Self {
        Self::RecvError(e)
    }
}

impl From<Infallible> for FaucetError {
    fn from(e: Infallible) -> Self {
        Self::Infallible(e)
    }
}

impl From<deadpool::managed::BuildError> for FaucetError {
    fn from(e: deadpool::managed::BuildError) -> Self {
        Self::PoolBuild(e)
    }
}

impl From<std::io::Error> for FaucetError {
    fn from(e: std::io::Error) -> Self {
        Self::Io(e)
    }
}

impl From<std::net::AddrParseError> for FaucetError {
    fn from(e: std::net::AddrParseError) -> Self {
        Self::HostParseError(e)
    }
}

impl From<hyper::Error> for FaucetError {
    fn from(e: hyper::Error) -> Self {
        Self::Hyper(e)
    }
}

impl std::fmt::Display for FaucetError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Self::RecvError(e) => write!(f, "Recv error: {}", e),
            Self::PoolBuild(e) => write!(f, "Pool build error: {}", e),
            Self::PoolTimeout(e) => write!(f, "Pool timeout error: {:?}", e),
            Self::PoolPostCreateHook => write!(f, "Pool post create hook error"),
            Self::PoolClosed => write!(f, "Pool closed error"),
            Self::PoolNoRuntimeSpecified => write!(f, "Pool no runtime specified error"),
            Self::Io(e) => write!(f, "IO error: {}", e),
            Self::Unknown(e) => write!(f, "Unknown error: {}", e),
            Self::HostParseError(e) => write!(f, "Error parsing host address: {}", e),
            Self::Hyper(e) => write!(f, "Hyper error: {}", e),
            Self::Infallible(e) => write!(f, "Infallible error: {}", e),
            Self::Http(e) => write!(f, "Http error: {}", e),
            Self::InvalidHeaderValues(e) => write!(f, "Invalid header values: {}", e),
            Self::BadRequest(r) => match r {
                BadRequestReason::MissingHeader(header) => {
                    write!(f, "Missing header: {}", header)
                }
                BadRequestReason::InvalidHeader(header) => {
                    write!(f, "Invalid header: {}", header)
                }
            },
        }
    }
}

impl std::fmt::Debug for FaucetError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Self::RecvError(e) => write!(f, "Recv error: {:?}", e),
            Self::PoolTimeout(e) => write!(f, "Pool timeout error: {:?}", e),
            Self::PoolPostCreateHook => write!(f, "Pool post create hook error"),
            Self::PoolClosed => write!(f, "Pool closed error"),
            Self::PoolNoRuntimeSpecified => write!(f, "Pool no runtime specified error"),
            Self::PoolBuild(e) => write!(f, "Pool build error: {:?}", e),
            Self::Io(e) => write!(f, "IO error: {:?}", e),
            Self::Unknown(e) => write!(f, "Unknown error: {:?}", e),
            Self::HostParseError(e) => write!(f, "Error parsing host address: {:?}", e),
            Self::Hyper(e) => write!(f, "Hyper error: {:?}", e),
            Self::Infallible(e) => write!(f, "Infallible error: {:?}", e),
            Self::Http(e) => write!(f, "Http error: {:?}", e),
            Self::InvalidHeaderValues(e) => write!(f, "Invalid header values: {:?}", e),
            Self::BadRequest(r) => match r {
                BadRequestReason::MissingHeader(header) => {
                    write!(f, "Missing header: {}", header)
                }
                BadRequestReason::InvalidHeader(header) => {
                    write!(f, "Invalid header: {}", header)
                }
            },
        }
    }
}

impl std::error::Error for FaucetError {}

impl FaucetError {
    pub fn no_sec_web_socket_key() -> Self {
        Self::BadRequest(BadRequestReason::MissingHeader("Sec-WebSocket-Key"))
    }
    pub fn unknown(s: impl ToString) -> Self {
        Self::Unknown(s.to_string())
    }
}

impl From<FaucetError> for hyper::Response<ExclusiveBody> {
    fn from(val: FaucetError) -> Self {
        let mut resp = hyper::Response::new(ExclusiveBody::plain_text(val.to_string()));
        *resp.status_mut() = hyper::StatusCode::INTERNAL_SERVER_ERROR;
        resp
    }
}