tower_web/extract/
error.rs1use self::Kind::*;
2
3use http::status::StatusCode;
4
5#[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 pub fn missing_argument() -> Error {
21 Error { kind: Missing }
22 }
23
24 pub fn is_missing_argument(&self) -> bool {
27 match self.kind {
28 Missing => true,
29 _ => false,
30 }
31 }
32
33 pub fn invalid_argument<T: ToString>(reason: &T) -> Error {
35 Error { kind: Invalid(reason.to_string()) }
36 }
37
38 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}