aboutsummaryrefslogtreecommitdiff
path: root/src/error.rs
blob: 3950b8004aadd72d582e240566c9bfab893b6551 (plain)
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
#[derive(Debug, thiserror::Error)]
pub enum ServiceError {
    #[error("no response generated; the proxy is misconfigured")]
    NoResponse,
    #[error("request taken; the proxy is misconfigured")]
    RequestTaken,
    #[error("limit reached. try again")]
    Limit(#[from] tokio::sync::TryAcquireError),
    #[error("hyper error")]
    Hyper(hyper::Error),
    #[error("h3 error: {0}")]
    H3(h3::Error),
    #[error("host header missing")]
    NoHost,
    #[error("unknown host")]
    UnknownHost,
    #[error("unknown path")]
    UnknownPath,
    #[error("can't connect to the backend")]
    CantConnect,
    #[error("not found")]
    NotFound,
    #[error("io error: {0}")]
    Io(#[from] std::io::Error),
    #[error("bad range")]
    BadRange,
    #[error("bad utf8")]
    BadUtf8(#[from] std::str::Utf8Error),
    #[error("bad utf8")]
    BadUtf82(#[from] std::string::FromUtf8Error),
    #[error("bad utf8")]
    BadUtf83(#[from] http::header::ToStrError),
    #[error("bad path")]
    BadPath,
    #[error("bad auth")]
    BadAuth,
    #[error("bad base64: {0}")]
    BadBase64(#[from] base64::DecodeError),
    #[error("connection upgrade failed")]
    UpgradeFailed,
    #[error("{0}")]
    Custom(String),
    #[error("{0}")]
    CustomStatic(&'static str),
    #[error("parse int error: {0}")]
    ParseIntError(#[from] std::num::ParseIntError),
    #[error("invalid header")]
    InvalidHeader,
    #[error("invalid uri")]
    InvalidUri,
    #[error("impossible error")]
    Other,
}

impl ServiceError {
    pub fn status_code(&self) -> http::StatusCode {
        use http::StatusCode;
        match self {
            ServiceError::NoResponse => todo!(),
            ServiceError::RequestTaken => StatusCode::INTERNAL_SERVER_ERROR,
            ServiceError::Limit(_) => StatusCode::TOO_MANY_REQUESTS,
            ServiceError::Hyper(_) => StatusCode::INTERNAL_SERVER_ERROR,
            ServiceError::H3(_) => StatusCode::INTERNAL_SERVER_ERROR,
            ServiceError::NoHost => StatusCode::BAD_REQUEST,
            ServiceError::UnknownHost => StatusCode::NOT_FOUND,
            ServiceError::UnknownPath => StatusCode::NOT_FOUND,
            ServiceError::CantConnect => StatusCode::BAD_GATEWAY,
            ServiceError::NotFound => StatusCode::NOT_FOUND,
            ServiceError::Io(_) => StatusCode::BAD_REQUEST,
            ServiceError::BadRange => StatusCode::BAD_REQUEST,
            ServiceError::BadUtf8(_) => StatusCode::BAD_REQUEST,
            ServiceError::BadUtf82(_) => StatusCode::BAD_REQUEST,
            ServiceError::BadUtf83(_) => StatusCode::BAD_REQUEST,
            ServiceError::BadPath => StatusCode::BAD_REQUEST,
            ServiceError::BadAuth => StatusCode::UNAUTHORIZED,
            ServiceError::BadBase64(_) => StatusCode::BAD_REQUEST,
            ServiceError::UpgradeFailed => StatusCode::UPGRADE_REQUIRED,
            ServiceError::Custom(_) => StatusCode::BAD_REQUEST,
            ServiceError::CustomStatic(_) => StatusCode::BAD_REQUEST,
            ServiceError::ParseIntError(_) => StatusCode::BAD_REQUEST,
            ServiceError::InvalidHeader => StatusCode::BAD_REQUEST,
            ServiceError::InvalidUri => StatusCode::BAD_REQUEST,
            ServiceError::Other => StatusCode::INTERNAL_SERVER_ERROR,
        }
    }
}