simple_hyper_client/
error.rs

1/* Copyright (c) Fortanix, Inc.
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7use hyper::Method;
8
9use std::{error, fmt};
10
11#[derive(Debug)]
12pub enum Error {
13    Http(http::Error),
14    Hyper(hyper::Error),
15    BodyNotAllowed(Method),
16}
17
18impl From<http::Error> for Error {
19    fn from(e: http::Error) -> Self {
20        Error::Http(e)
21    }
22}
23
24impl From<hyper::Error> for Error {
25    fn from(e: hyper::Error) -> Self {
26        Error::Hyper(e)
27    }
28}
29
30impl fmt::Display for Error {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match *self {
33            Error::Http(ref e) => write!(f, "{}", e),
34            Error::Hyper(ref e) => write!(f, "{}", e),
35            Error::BodyNotAllowed(ref m) => {
36                write!(f, "{} requests are not allowed to have a body", m)
37            }
38        }
39    }
40}
41
42impl error::Error for Error {
43    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
44        match *self {
45            Error::Http(ref e) => Some(e),
46            Error::Hyper(ref e) => Some(e),
47            Error::BodyNotAllowed(_) => None,
48        }
49    }
50}