Add internal error template

This commit is contained in:
traxys 2023-08-15 19:46:26 +02:00
parent e6616c75af
commit 054a620664
4 changed files with 43 additions and 7 deletions

View file

@ -12,6 +12,7 @@ use axum::{
Router,
};
use color_eyre::eyre;
use once_cell::sync::Lazy;
use openidconnect::{
core::{CoreAuthenticationFlow, CoreClient, CoreProviderMetadata},
url::Url,
@ -263,7 +264,6 @@ impl OpenidConnector {
struct AppState {
db: PgPool,
oidc: OpenidConnector,
tera: Tera,
}
#[derive(thiserror::Error, Debug)]
@ -280,7 +280,17 @@ struct InternalError;
impl IntoResponse for InternalError {
fn into_response(self) -> axum::response::Response {
(StatusCode::INTERNAL_SERVER_ERROR, "Internal Error").into_response()
(
StatusCode::INTERNAL_SERVER_ERROR,
match TEMPLATES.render("error.html", &global_context()) {
Ok(v) => Html(v).into_response(),
Err(e) => {
tracing::error!("Could not generate internal error: {e:?}");
"Internal Error".into_response()
}
},
)
.into_response()
}
}
@ -353,13 +363,20 @@ fn global_context() -> tera::Context {
ctx
}
async fn page_not_found(state: State<Arc<AppState>>) -> Result<(StatusCode, Html<String>), Error> {
async fn page_not_found() -> Result<(StatusCode, Html<String>), Error> {
Ok((
StatusCode::NOT_FOUND,
Html(state.tera.render("not_found.html", &global_context())?),
Html(TEMPLATES.render("not_found.html", &global_context())?),
))
}
async fn error() -> Error {
Error::InternalError
}
pub static TEMPLATES: Lazy<Tera> =
Lazy::new(|| Tera::new("templates/*.html").expect("Could not generate templates"));
#[tokio::main]
async fn main() -> color_eyre::Result<()> {
color_eyre::install()?;
@ -384,15 +401,13 @@ async fn main() -> color_eyre::Result<()> {
sqlx::migrate!().run(&db).await?;
let tera = Tera::new("templates/*.html")?;
tracing::info!("Listening on {addr}");
let router = Router::new()
.route("/login", get(login))
.route("/login/redirect/:id", get(redirected))
.fallback(page_not_found)
.with_state(Arc::new(AppState { db, oidc, tera }));
.with_state(Arc::new(AppState { db, oidc }));
Ok(axum::Server::bind(&addr)
.serve(router.into_make_service())