slightly improve error handling in handlers

This commit is contained in:
Armaël Guéneau 2025-01-02 12:13:27 +01:00
parent 00cb127953
commit aef35f2a0b

View file

@ -4,8 +4,7 @@ use axum::{
http::{header, StatusCode},
response::{Html, IntoResponse},
routing::get,
Form,
Router,
Form, Router,
};
use forgejo_api::{Auth, Forgejo};
use lazy_static::lazy_static;
@ -299,7 +298,7 @@ async fn get_index(
State(data): State<Arc<AppState>>,
Query(q): Query<SortSetting>,
OriginalUri(uri): OriginalUri,
) -> Html<String> {
) -> Result<Html<String>, AppError> {
eprintln!("GET {}", uri);
let db = &data.db;
@ -355,9 +354,9 @@ async fn get_index(
);
context.insert("total_users_count", &users_count);
eprintln!("rendering template...");
let page = TEMPLATES.render("index.html", &context).unwrap();
let page = TEMPLATES.render("index.html", &context)?;
eprintln!("done");
Html::from(page)
Ok(Html::from(page))
}
async fn post_classified(
@ -387,7 +386,11 @@ async fn post_classified(
eprintln!("done");
Ok((StatusCode::SEE_OTHER, [(header::LOCATION, uri.to_string())], ()))
Ok((
StatusCode::SEE_OTHER,
[(header::LOCATION, uri.to_string())],
(),
))
}
async fn post_classified_index(
@ -409,7 +412,7 @@ async fn post_classified_edit(
async fn get_classified(
State(data): State<Arc<AppState>>,
OriginalUri(uri): OriginalUri,
) -> Html<String> {
) -> Result<Html<String>, AppError> {
eprintln!("GET {}", uri);
let db = &data.db;
@ -431,14 +434,17 @@ async fn get_classified(
context.insert("forge_url", &data.config.forge_url.to_string());
context.insert("users", &users);
eprintln!("rendering template...");
let page = TEMPLATES.render("classified.html", &context).unwrap();
let page = TEMPLATES.render("classified.html", &context)?;
eprintln!("done");
Html::from(page)
Ok(Html::from(page))
}
const STATIC_DIR: include_dir::Dir = include_dir::include_dir!("static");
async fn get_static_(Path(filename): Path<String>, OriginalUri(uri): OriginalUri) -> impl IntoResponse {
async fn get_static_(
Path(filename): Path<String>,
OriginalUri(uri): OriginalUri,
) -> impl IntoResponse {
eprintln!("GET {}", uri);
match STATIC_DIR.get_file(filename) {
@ -498,7 +504,10 @@ async fn main() -> anyhow::Result<()> {
let app = Router::new()
.route("/", get(get_index).post(post_classified_index))
.route("/classified", get(get_classified).post(post_classified_edit))
.route(
"/classified",
get(get_classified).post(post_classified_edit),
)
.route("/static/{*filename}", get(get_static_))
.with_state(shared_state);