diff --git a/src/index.rs b/src/index.rs index 1b68e3f..e44f0e7 100644 --- a/src/index.rs +++ b/src/index.rs @@ -1,9 +1,10 @@ +use anyhow::Context; use axum::{extract::State, response::Html}; use http::StatusCode; use serde::Deserialize; use tokio::process::Command; -use crate::SharedState; +use crate::{SharedState, util::e}; #[derive(Clone, Debug, Deserialize)] struct FlatpakApp { @@ -28,10 +29,8 @@ pub async fn index(State(state): State) -> Result, Sta .arg(format!("file://{}", repo.display())) .output() .await - .map_err(|e| { - tracing::error!("Failed to execute `flatpak remote-ls`: {e:?}"); - StatusCode::INTERNAL_SERVER_ERROR - })? + .context("Failed to execute `flatpak remote-ls`") + .map_err(e(StatusCode::INTERNAL_SERVER_ERROR))? }; let stdout = String::from_utf8_lossy(&output.stdout); @@ -42,10 +41,9 @@ pub async fn index(State(state): State) -> Result, Sta tracing::error!("stderr:\n{stderr}\n"); } - let list: Vec = serde_json::from_str(&stdout).map_err(|e| { - tracing::error!("Failed to deserialize `flatpak remote-ls --json`'s output: {e:?}"); - StatusCode::INTERNAL_SERVER_ERROR - })?; + let list: Vec = serde_json::from_str(&stdout) + .context("Failed to deserialize `flatpak remote-ls --json`'s output") + .map_err(e(StatusCode::INTERNAL_SERVER_ERROR))?; Ok(to_html(&list)) } diff --git a/src/util.rs b/src/util.rs index bda1bd3..9d7d7bb 100644 --- a/src/util.rs +++ b/src/util.rs @@ -2,6 +2,9 @@ use std::fmt::Display; use http::StatusCode; +/// Helper funciton for use with [`Result::map_err`]. +/// +/// Logs the error and replaces it with the provided status code. #[track_caller] pub fn e(code: StatusCode) -> impl FnOnce(E) -> StatusCode { move |e| {