accept_encoding_fork/
error.rs

1use failure::{self, Backtrace, Context, Fail};
2use std::fmt::{self, Display};
3use std::result;
4
5/// A specialized [`Result`] type for this crate's operations.
6///
7/// This is generally used to avoid writing out [Error] directly and
8/// is otherwise a direct mapping to [`Result`].
9///
10/// [`Result`]: https://doc.rust-lang.org/nightly/std/result/enum.Result.html
11/// [`Error`]: std.struct.Error.html
12pub type Result<T> = result::Result<T, failure::Error>;
13
14/// A list enumerating the categories of errors in this crate.
15///
16/// This list is intended to grow over time and it is not recommended to
17/// exhaustively match against it.
18///
19/// It is used with the [`Error`] struct.
20///
21/// [`Error`]: std.struct.Error.html
22#[derive(Debug, Fail)]
23pub enum ErrorKind {
24    /// Invalid header encoding.
25    #[fail(display = "Invalid header encoding.")]
26    InvalidEncoding,
27    /// The encoding scheme is unknown.
28    #[fail(display = "Unknown encoding scheme.")]
29    UnknownEncoding,
30    /// Any error not part of this list.
31    #[fail(display = "Generic error.")]
32    Other,
33}
34
35/// A specialized [`Error`] type for this crate's operations.
36///
37/// [`Error`]: https://doc.rust-lang.org/nightly/std/error/trait.Error.html
38#[derive(Debug)]
39pub struct Error {
40    inner: Context<ErrorKind>,
41}
42
43impl Error {
44    /// Access the [`ErrorKind`] member.
45    ///
46    /// [`ErrorKind`]: enum.ErrorKind.html
47    pub fn kind(&self) -> &ErrorKind {
48        &*self.inner.get_context()
49    }
50}
51
52impl Fail for Error {
53    fn cause(&self) -> Option<&dyn Fail> {
54        self.inner.cause()
55    }
56
57    fn backtrace(&self) -> Option<&Backtrace> {
58        self.inner.backtrace()
59    }
60}
61
62impl Display for Error {
63    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64        Display::fmt(&self.inner, f)
65    }
66}
67
68impl From<ErrorKind> for Error {
69    fn from(kind: ErrorKind) -> Error {
70        let inner = Context::new(kind);
71        Error { inner }
72    }
73}
74
75impl From<Context<ErrorKind>> for Error {
76    fn from(inner: Context<ErrorKind>) -> Error {
77        Error { inner }
78    }
79}