aboutsummaryrefslogtreecommitdiff
path: root/src/error.rs
blob: 0a36476e0ea1aad05345cff343a1fe705b08a8b1 (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
use sputnik::hyper_body::FormError;
use sputnik::request::QueryError;
use std::str::Utf8Error;

/// For convenience this enum also contains nonerroneous variants.
pub enum Error {
    /// A 400 bad request error.
    BadRequest(String),
    /// A 401 unauthorized error.
    Unauthorized(String),
    /// A 403 forbidden error.
    Forbidden(String),
    /// A 404 not found error.
    NotFound(String),
    /// A 500 internal server error.
    Internal(String),
    /// A 304 response.
    NotModified,
    /// A 302 redirect to the given path.
    Redirect(String),

    // TODO: use Redirect instead
    /// Missing trailing slash.
    MissingTrailingSlash(String),
}

impl From<Utf8Error> for Error {
    fn from(_: Utf8Error) -> Self {
        Self::BadRequest("invalid UTF-8".into())
    }
}

impl From<git2::Error> for Error {
    fn from(e: git2::Error) -> Self {
        eprintln!("git error: {}", e);
        Self::Internal("something went wrong with git".into())
    }
}

impl From<FormError> for Error {
    fn from(e: FormError) -> Self {
        Self::BadRequest(e.to_string())
    }
}

impl From<QueryError> for Error {
    fn from(e: QueryError) -> Self {
        Self::BadRequest(e.to_string())
    }
}