Use error helper e in index.rs
This commit is contained in:
+7
-9
@@ -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<SharedState>) -> Result<Html<String>, 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<SharedState>) -> Result<Html<String>, Sta
|
||||
tracing::error!("stderr:\n{stderr}\n");
|
||||
}
|
||||
|
||||
let list: Vec<FlatpakApp> = 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<FlatpakApp> = 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))
|
||||
}
|
||||
|
||||
@@ -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<E: Display>(code: StatusCode) -> impl FnOnce(E) -> StatusCode {
|
||||
move |e| {
|
||||
|
||||
Reference in New Issue
Block a user