error messages

This commit is contained in:
Priec
2026-08-15 15:08:35 +02:00
parent d0bcdb1b7c
commit fe3848c453
10 changed files with 395 additions and 348 deletions

View File

@@ -62,3 +62,19 @@ pub(crate) enum LoadError {
Forbidden,
Backend(String),
}
impl LoadError {
/// Hands the page load's failure to the one place that decides statuses.
/// `permission_key` names the message shown when the load was refused.
pub(crate) fn into_form_error(
self,
locale: crate::i18n::Locale,
permission_key: &str,
) -> crate::ui::FormError {
match self {
Self::Unauthenticated => crate::ui::FormError::Unauthenticated,
Self::Forbidden => crate::ui::FormError::Forbidden(locale.lookup(permission_key)),
Self::Backend(message) => crate::ui::FormError::Unavailable(message),
}
}
}

View File

@@ -1,14 +1,15 @@
use axum::{
Form,
extract::State,
http::{HeaderMap, StatusCode},
http::HeaderMap,
response::{Html, IntoResponse, Redirect, Response},
};
use crate::{
AppState,
{i18n::Locale, tr},
i18n::Locale,
services::{authenticated_request, reject_cross_site},
ui::FormError,
};
use super::{
@@ -38,12 +39,7 @@ pub(crate) async fn create_logic(
}
let request = match form.into_request(Locale::from_headers(&headers)) {
Ok(request) => request,
Err(message) => {
return render_loaded(
&headers,
load_page(state, &headers, submitted, Some(message)).await,
);
}
Err(message) => return reject(&headers, message),
};
let mut scripts = state.scripts;
let request = match authenticated_request(&headers, request) {
@@ -57,34 +53,25 @@ pub(crate) async fn create_logic(
&response.get_ref().warnings,
))
.into_response(),
Err(error) => (
StatusCode::UNPROCESSABLE_ENTITY,
Html(ui::render_submission_error(
Locale::from_headers(&headers),
error.message(),
)),
)
.into_response(),
Err(error) => FormError::from_status(&error)
.into_response(Locale::from_headers(&headers), ui::render_submission_error),
}
}
/// A submission this crate rejected before it reached the backend. Same
/// response as a backend rejection: the user has to change something either
/// way, and the form's `hx-target` is the status block in both cases.
fn reject(headers: &HeaderMap, message: String) -> Response {
FormError::Rejected(message)
.into_response(Locale::from_headers(headers), ui::render_submission_error)
}
fn render_loaded(headers: &HeaderMap, result: Result<super::state::AddLogicPageState, LoadError>) -> Response {
let locale = Locale::from_headers(headers);
match result {
Ok(page) => Html(ui::render_page(&page)).into_response(),
Err(LoadError::Unauthenticated) => Redirect::to("/login").into_response(),
Err(LoadError::Forbidden) => {
let message = tr!(locale, "add-logic-err-permission");
(
StatusCode::FORBIDDEN,
Html(ui::render_submission_error(locale, &message)),
)
.into_response()
}
Err(LoadError::Backend(message)) => (
StatusCode::BAD_GATEWAY,
Html(ui::render_submission_error(locale, &message)),
)
.into_response(),
Err(error) => error
.into_form_error(locale, "add-logic-err-permission")
.into_response(locale, ui::render_submission_error),
}
}