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
//! Contains the error type used by `Server`

use crate::BoxError;

use std::net::AddrParseError;
use thiserror::Error;

/// Error returned by the [`Server.listen`](crate::Server::listen()) method
#[derive(Error, Debug)]
#[error("server error: {msg}")]
pub struct ServerError {
    msg: String,
    #[source]
    source: BoxError,
}

impl ServerError {
    fn new<E: std::error::Error + Send + Sync + 'static>(msg: impl Into<String>, source: E) -> ServerError {
        ServerError {
            msg: msg.into(),
            source: Box::new(source),
        }
    }
}

impl From<AddrParseError> for ServerError {
    fn from(e: AddrParseError) -> Self {
        ServerError::new("could not parse address", e)
    }
}

impl From<std::io::Error> for ServerError {
    fn from(e: std::io::Error) -> Self {
        ServerError::new("io error", e)
    }
}

impl From<super::tls::ConfigError> for ServerError {
    fn from(e: super::tls::ConfigError) -> Self {
        ServerError::new(format!("error with TLS configuration: {}", e), e)
    }
}

#[derive(Error, Debug)]
#[error("shutdown error: {msg}")]
pub struct ShutdownError {
    pub msg: String,
}

impl From<ShutdownError> for ServerError {
    fn from(e: ShutdownError) -> Self {
        ServerError::new("shutdown error", e)
    }
}