Skip to main content

apimock_server/
error.rs

1//! Errors produced by server-level operations.
2//!
3//! See `apimock_routing::error` for the rationale on per-crate error
4//! types. `ServerError` wraps `ConfigError` via `#[from]` because
5//! server startup calls `Config::new` on the user's behalf.
6
7use std::{io, path::PathBuf};
8
9pub type ServerResult<T> = Result<T, ServerError>;
10
11#[derive(Debug, thiserror::Error)]
12pub enum ServerError {
13    /// TLS certificate or private key failed to load.
14    #[error("TLS material load failed ({kind}) at `{path}`: {reason}")]
15    TlsLoad {
16        kind: TlsKind,
17        path: PathBuf,
18        reason: String,
19    },
20
21    /// Listener address failed to resolve or bind.
22    #[error("invalid listener address `{addr}`: {reason}")]
23    ListenerAddress { addr: String, reason: String },
24
25    /// A middleware file listed in config was missing on disk.
26    #[error("middleware script not found: `{path}`")]
27    MiddlewareMissing { path: PathBuf },
28
29    /// A middleware file was found but failed to compile.
30    #[error("failed to compile middleware `{path}`: {reason}")]
31    MiddlewareCompile { path: PathBuf, reason: String },
32
33    /// Catch-all for plain I/O that doesn't have a more specific variant.
34    #[error("i/o error: {0}")]
35    Io(#[from] io::Error),
36
37    /// Forwarded from the config crate when server startup triggers
38    /// config loading.
39    #[error(transparent)]
40    Config(#[from] apimock_config::ConfigError),
41}
42
43#[derive(Debug, Clone, Copy)]
44pub enum TlsKind {
45    Certificate,
46    PrivateKey,
47}
48
49impl std::fmt::Display for TlsKind {
50    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
51        match self {
52            TlsKind::Certificate => f.write_str("certificate"),
53            TlsKind::PrivateKey => f.write_str("private key"),
54        }
55    }
56}