Skip to main content

kerniq_core/
lib.rs

1//! Core types shared by every Kerniq Server crate.
2//!
3//! This crate sits at the bottom of the dependency graph and contains only
4//! what every other crate needs:
5//!
6//! * [`Error`] / [`Result`] — the framework error model and its JSON
7//!   envelope ([`error`] module).
8//! * [`ServerConfig`] — programmatic, environment and file configuration
9//!   ([`config`] module).
10//! * [`ServerError`] — failures while starting or running the server.
11//!
12//! Application code normally uses these through `kerniq_server::prelude`.
13
14#![warn(missing_docs)]
15#![cfg_attr(docsrs, feature(doc_cfg))]
16
17pub mod config;
18pub mod error;
19
20pub use config::{ConfigError, ServerConfig};
21pub use error::{BoxError, Error, OptionExt, Result, ResultExt};
22
23/// Errors raised while starting or running the HTTP server (as opposed to
24/// errors produced while handling a request).
25#[derive(Debug, thiserror::Error)]
26#[non_exhaustive]
27pub enum ServerError {
28    /// The listen address could not be parsed or resolved.
29    #[error("invalid listen address `{address}`: {reason}")]
30    InvalidAddress {
31        /// The address as given.
32        address: String,
33        /// Why it was rejected.
34        reason: String,
35    },
36    /// Binding the TCP listener failed (address in use, permission denied...).
37    #[error("failed to bind to `{address}`: {source}")]
38    Bind {
39        /// The address that could not be bound.
40        address: String,
41        /// The underlying I/O error.
42        #[source]
43        source: std::io::Error,
44    },
45    /// `run_blocking` was called from inside an active Tokio runtime.
46    #[error(
47        "`run_blocking` cannot be called from inside a Tokio runtime (it would create a nested \
48         runtime). Inside `#[tokio::main]` or any async context, use `app.run(addr).await` instead"
49    )]
50    NestedRuntime,
51    /// The Tokio runtime could not be created.
52    #[error("failed to build the Tokio runtime: {0}")]
53    Runtime(#[source] std::io::Error),
54    /// An I/O error occurred on the listening socket.
55    #[error("server I/O error: {0}")]
56    Io(#[from] std::io::Error),
57}