Browse Source

Improve errors

ng
Rodolphe Bréard 3 weeks ago
parent
commit
11278ce8da
Failed to extract signature
  1. 28
      src/http.rs
  2. 5
      src/main.rs

28
src/http.rs

@ -21,7 +21,8 @@ pub struct HttpClient {
}
impl HttpClient {
#[tracing::instrument(skip(self), level = "trace", err(Debug))]
// TODO: err(Alternate)
#[tracing::instrument(skip(self), name = "http request", level = "trace", err)]
pub async fn send<S: AsRef<str> + std::fmt::Debug>(
&self,
endpoint: S,
@ -52,16 +53,16 @@ pub(super) struct HttpRoutine {
impl HttpRoutine {
#[tracing::instrument(skip_all, level = "trace")]
pub(super) fn new(config: &AcmedConfig) -> Self {
pub(super) fn new(config: &AcmedConfig) -> Result<Self> {
let (tx, rx) = mpsc::unbounded_channel();
let mut endpoints = HashMap::with_capacity(config.endpoint.len());
for (name, edp) in &config.endpoint {
tracing::debug!("endpoint name" = name, "loading endpoint");
let client = get_http_client(config.get_global_root_certs(), &edp.root_certificates);
let client = get_http_client(config.get_global_root_certs(), &edp.root_certificates)?;
let endpoint = HttpEndpoint { client };
endpoints.insert(name.to_owned(), endpoint);
}
Self { tx, rx, endpoints }
Ok(Self { tx, rx, endpoints })
}
pub(super) fn get_client(&self) -> HttpClient {
@ -92,19 +93,17 @@ impl HttpRoutine {
macro_rules! add_root_cert {
($builder: ident, $iter: ident) => {
for cert_path in $iter {
match get_cert_pem(cert_path) {
Ok(cert) => {
$builder = $builder.add_root_certificate(cert);
tracing::debug!("path" = %cert_path.display(), "root certificate loaded");
}
Err(e) => tracing::error!(path = ?cert_path, error = "{e:#?}", "unable to load root certificate"),
if let Ok(cert) = get_cert_pem(cert_path) {
$builder = $builder.add_root_certificate(cert);
tracing::debug!("path" = %cert_path.display(), "root certificate loaded");
}
}
};
}
#[tracing::instrument(level = "trace")]
fn get_http_client(base_certs_opt: Option<&[PathBuf]>, end_certs: &[PathBuf]) -> Client {
// TODO: err(Alternate)
#[tracing::instrument(level = "trace", err)]
fn get_http_client(base_certs_opt: Option<&[PathBuf]>, end_certs: &[PathBuf]) -> Result<Client> {
let useragent = format!(
"{}/{} ({}) {}",
env!("CARGO_BIN_NAME"),
@ -121,9 +120,12 @@ fn get_http_client(base_certs_opt: Option<&[PathBuf]>, end_certs: &[PathBuf]) ->
add_root_cert!(client_builder, base_certs);
}
add_root_cert!(client_builder, end_certs);
client_builder.build().unwrap()
let client = client_builder.build()?;
Ok(client)
}
// TODO: err(Alternate)
#[tracing::instrument(name = "load root certificate", level = "trace", err)]
fn get_cert_pem(cert_path: &Path) -> Result<Certificate> {
let mut buff = Vec::new();
File::open(cert_path)?.read_to_end(&mut buff)?;

5
src/main.rs

@ -54,7 +54,7 @@ async fn start(cnf: AcmedConfig) {
tracing::info!("starting ACMEd");
// Start the HTTP routine
let http_routine = HttpRoutine::new(&cnf);
let http_routine = HttpRoutine::new(&cnf).expect("unable to load the http client");
let http_client = http_routine.get_client();
tokio::spawn(async move {
http_routine.run().await;
@ -87,7 +87,8 @@ async fn debug_remove_me(http_client: crate::http::HttpClient) {
tracing::debug!("response received" = ?rsp);
}
#[tracing::instrument(level = "trace", err(Debug))]
// TODO: err(Alternate)
#[tracing::instrument(level = "trace", err)]
fn init_server(foreground: bool, pid_file: Option<&Path>) -> Result<()> {
if !foreground {
let mut daemonize = Daemonize::new();

Loading…
Cancel
Save