Skip to main content

httpsd/
error.rs

1//! The crate-wide error type.
2
3use std::fmt;
4
5/// Errors produced by the HTTP engine, the runtimes, and configuration loading.
6#[derive(Debug)]
7#[non_exhaustive]
8pub enum Error {
9    /// The peer sent a malformed request (bad request line, headers, or body
10    /// framing). Carries a human-readable reason.
11    BadRequest(&'static str),
12    /// A request exceeded a configured limit (header bytes, body bytes, …).
13    TooLarge(&'static str),
14    /// An I/O error from a socket or the filesystem.
15    Io(std::io::Error),
16    /// A TLS-layer error (handshake failure, decrypt error, …). Only produced
17    /// when the `tls` feature is enabled.
18    Tls(String),
19    /// A compression-layer error. Only produced when the `compress` feature is
20    /// enabled.
21    Compress(String),
22    /// A configuration error (invalid TOML, missing file, bad value, …).
23    Config(String),
24    /// An ACME certificate-management error (protocol, network, or CA error).
25    Acme(String),
26}
27
28impl fmt::Display for Error {
29    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30        match self {
31            Error::BadRequest(why) => write!(f, "bad request: {why}"),
32            Error::TooLarge(what) => write!(f, "request too large: {what}"),
33            Error::Io(e) => write!(f, "io error: {e}"),
34            Error::Tls(e) => write!(f, "tls error: {e}"),
35            Error::Compress(e) => write!(f, "compression error: {e}"),
36            Error::Config(e) => write!(f, "config error: {e}"),
37            Error::Acme(e) => write!(f, "acme error: {e}"),
38        }
39    }
40}
41
42impl std::error::Error for Error {
43    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
44        match self {
45            Error::Io(e) => Some(e),
46            _ => None,
47        }
48    }
49}
50
51impl From<std::io::Error> for Error {
52    fn from(e: std::io::Error) -> Self {
53        Error::Io(e)
54    }
55}
56
57/// Convenience alias used throughout the crate.
58pub type Result<T> = std::result::Result<T, Error>;