tower_web/extract/
error.rs

1use self::Kind::*;
2
3use http::status::StatusCode;
4
5/// Errors that can happen while extracting data from an HTTP request.
6#[derive(Debug)]
7pub struct Error {
8    kind: Kind,
9}
10
11#[derive(Debug)]
12enum Kind {
13    Missing,
14    Invalid(String),
15    Web(::Error),
16}
17
18impl Error {
19    /// The data is missing from the HTTP request.
20    pub fn missing_argument() -> Error {
21        Error { kind: Missing }
22    }
23
24    /// Returns `true` when the error represents missing data from the HTTP
25    /// request.
26    pub fn is_missing_argument(&self) -> bool {
27        match self.kind {
28            Missing => true,
29            _ => false,
30        }
31    }
32
33    /// The data is in an invalid format and cannot be extracted.
34    pub fn invalid_argument<T: ToString>(reason: &T) -> Error {
35        Error { kind: Invalid(reason.to_string()) }
36    }
37
38    /// Returns `true` when the data is in an invalid format and cannot be
39    /// extracted.
40    pub fn is_invalid_argument(&self) -> bool {
41        match self.kind {
42            Invalid(_) => true,
43            _ => false,
44        }
45    }
46
47    pub(crate) fn internal_error() -> Error {
48        ::Error::from(StatusCode::BAD_REQUEST).into()
49    }
50}
51
52impl From<Error> for ::Error {
53    fn from(err: Error) -> Self {
54        match err.kind {
55            Missing | Invalid(_) => ::Error::from(StatusCode::BAD_REQUEST),
56            Web(err) => err,
57        }
58    }
59}
60
61impl From<::Error> for Error {
62    fn from(err: ::Error) -> Self {
63        Error { kind: Web(err) }
64    }
65}