1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#![cfg_attr(feature = "cargo-clippy", allow(stutter))]

use std;
use std::fmt::{self, Display};

/// An error when creating the [`Service`] from a [`Breadboard`].
///
/// [`Service`]: ../hyper/service/trait.Service.html
/// [`Breadboard`]: struct.Breadboard.html
#[allow(missing_copy_implementations)]
#[cfg_attr(feature = "cargo-clippy", allow(empty_enum))]
#[derive(Debug)]
pub enum BuildError {}

impl Display for BuildError {
    fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {}
    }
}

impl std::error::Error for BuildError {}

/// An error during request handling.
#[derive(Debug)]
pub enum RequestError {
    /// An error returned by the handler function.
    Handler(Box<dyn std::error::Error + Send + Sync>),
    /// An error that occurred during handling of the HTTP stream.
    Hyper(hyper::Error),
    /// An error that occurred when deserializing the request body.
    De(serde_json::Error),
    /// An error that occurred when serializing the response body.
    Ser(serde_json::Error),
}

impl Display for RequestError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            RequestError::Handler(e) => e.fmt(f),
            RequestError::Hyper(e) => e.fmt(f),
            RequestError::De(e) | RequestError::Ser(e) => e.fmt(f),
        }
    }
}

impl std::error::Error for RequestError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            RequestError::Handler(_) => None,
            RequestError::Hyper(e) => Some(e),
            RequestError::De(e) | RequestError::Ser(e) => Some(e),
        }
    }
}