distinct error types for running server

This is in preparation for the config change to allow specifying
multiple listeners, which will add several other possible error
conditions.
This commit is contained in:
Benjamin Lee 2024-06-08 12:21:07 -07:00
parent 76b060aa84
commit b7ad00ef6e
No known key found for this signature in database
GPG key ID: FB9624E2885D55A4
2 changed files with 32 additions and 8 deletions

View file

@ -48,7 +48,7 @@ pub(crate) enum Main {
DatabaseError(#[source] crate::utils::error::Error),
#[error("failed to serve requests")]
Serve(#[source] std::io::Error),
Serve(#[from] Serve),
}
/// Observability initialization errors
@ -97,3 +97,21 @@ pub(crate) enum ConfigSearch {
#[error("no relevant configuration files found in XDG Base Directories")]
NotFound,
}
/// Errors serving traffic
// Missing docs are allowed here since that kind of information should be
// encoded in the error messages themselves anyway.
#[allow(missing_docs)]
#[derive(Error, Debug)]
pub(crate) enum Serve {
#[error("failed to read TLS cert and key files at {certs:?} and {key:?}")]
LoadCerts {
certs: String,
key: String,
#[source]
err: std::io::Error,
},
#[error("failed to run request listener")]
Listen(#[source] std::io::Error),
}

View file

@ -1,6 +1,5 @@
use std::{
future::Future,
io,
net::SocketAddr,
process::ExitCode,
sync::{atomic, RwLock},
@ -127,12 +126,14 @@ async fn try_main() -> Result<(), error::Main> {
.map_err(Error::DatabaseError)?;
info!("Starting server");
run_server().await.map_err(Error::Serve)?;
run_server().await?;
Ok(())
}
async fn run_server() -> io::Result<()> {
async fn run_server() -> Result<(), error::Serve> {
use error::Serve as Error;
let config = &services().globals.config;
let addr = SocketAddr::from((config.address, config.port));
@ -189,15 +190,20 @@ async fn run_server() -> io::Result<()> {
match &config.tls {
Some(tls) => {
let conf =
RustlsConfig::from_pem_file(&tls.certs, &tls.key).await?;
let conf = RustlsConfig::from_pem_file(&tls.certs, &tls.key)
.await
.map_err(|err| Error::LoadCerts {
certs: tls.certs.clone(),
key: tls.key.clone(),
err,
})?;
let server = bind_rustls(addr, conf).handle(handle).serve(app);
#[cfg(feature = "systemd")]
sd_notify::notify(true, &[sd_notify::NotifyState::Ready])
.expect("should be able to notify systemd");
server.await?;
server.await.map_err(Error::Listen)?;
}
None => {
let server = bind(addr).handle(handle).serve(app);
@ -206,7 +212,7 @@ async fn run_server() -> io::Result<()> {
sd_notify::notify(true, &[sd_notify::NotifyState::Ready])
.expect("should be able to notify systemd");
server.await?;
server.await.map_err(Error::Listen)?;
}
}