hyperx/
error.rs

1//! Error and Result module.
2
3use std::error::Error as StdError;
4use std::fmt;
5use std::str::Utf8Error;
6use std::string::FromUtf8Error;
7
8use self::Error::{
9    Method,
10    Version,
11    Header,
12    Status,
13    TooLarge,
14    Utf8
15};
16
17/// Result type often returned from methods that can have hyper `Error`s.
18pub type Result<T> = ::std::result::Result<T, Error>;
19
20/// Errors while parsing headers and associated types.
21#[derive(Debug)]
22pub enum Error {
23    /// An invalid `Method`, such as `GE,T`.
24    Method,
25    /// An invalid `HttpVersion`, such as `HTP/1.1`
26    Version,
27    /// An invalid `Header`.
28    Header,
29    /// A message head is too large to be reasonable.
30    TooLarge,
31    /// An invalid `Status`, such as `1337 ELITE`.
32    Status,
33    /// Parsing a field as string failed.
34    Utf8(Utf8Error),
35
36    #[doc(hidden)]
37    __Nonexhaustive(Void)
38}
39
40#[doc(hidden)]
41pub struct Void(());
42
43impl fmt::Debug for Void {
44    fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
45        unreachable!()
46    }
47}
48
49impl fmt::Display for Error {
50    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
51        match *self {
52            Utf8(ref e) => fmt::Display::fmt(e, f),
53            ref e => f.write_str(e.static_description()),
54        }
55    }
56}
57
58impl Error {
59    fn static_description(&self) -> &str {
60        match *self {
61            Method => "invalid Method specified",
62            Version => "invalid HTTP version specified",
63            Header => "invalid Header provided",
64            TooLarge => "message head is too large",
65            Status => "invalid Status provided",
66            Utf8(_) => "invalid UTF-8 string",
67            Error::__Nonexhaustive(..) => unreachable!(),
68        }
69    }
70}
71
72impl StdError for Error {
73    fn description(&self) -> &str {
74        self.static_description()
75    }
76
77    fn cause(&self) -> Option<&dyn StdError> {
78        match *self {
79            Utf8(ref error) => Some(error),
80            Error::__Nonexhaustive(..) => unreachable!(),
81            _ => None,
82        }
83    }
84}
85
86impl From<Utf8Error> for Error {
87    fn from(err: Utf8Error) -> Error {
88        Utf8(err)
89    }
90}
91
92impl From<FromUtf8Error> for Error {
93    fn from(err: FromUtf8Error) -> Error {
94        Utf8(err.utf8_error())
95    }
96}
97
98#[doc(hidden)]
99trait AssertSendSync: Send + Sync + 'static {}
100#[doc(hidden)]
101impl AssertSendSync for Error {}