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

1
Cargo.lock generated
View file

@ -1121,6 +1121,7 @@ dependencies = [
"axum", "axum",
"color-eyre", "color-eyre",
"envious", "envious",
"once_cell",
"openidconnect", "openidconnect",
"parking_lot", "parking_lot",
"serde", "serde",

View file

@ -8,6 +8,7 @@ edition = "2021"
axum = { version = "0.6.20", features = ["query"] } axum = { version = "0.6.20", features = ["query"] }
color-eyre = "0.6.2" color-eyre = "0.6.2"
envious = "0.2.2" envious = "0.2.2"
once_cell = "1.18.0"
openidconnect = "3.3.0" openidconnect = "3.3.0"
parking_lot = "0.12.1" parking_lot = "0.12.1"
serde = { version = "1.0.183", features = ["derive"] } serde = { version = "1.0.183", features = ["derive"] }

View file

@ -12,6 +12,7 @@ use axum::{
Router, Router,
}; };
use color_eyre::eyre; use color_eyre::eyre;
use once_cell::sync::Lazy;
use openidconnect::{ use openidconnect::{
core::{CoreAuthenticationFlow, CoreClient, CoreProviderMetadata}, core::{CoreAuthenticationFlow, CoreClient, CoreProviderMetadata},
url::Url, url::Url,
@ -263,7 +264,6 @@ impl OpenidConnector {
struct AppState { struct AppState {
db: PgPool, db: PgPool,
oidc: OpenidConnector, oidc: OpenidConnector,
tera: Tera,
} }
#[derive(thiserror::Error, Debug)] #[derive(thiserror::Error, Debug)]
@ -280,7 +280,17 @@ struct InternalError;
impl IntoResponse for InternalError { impl IntoResponse for InternalError {
fn into_response(self) -> axum::response::Response { 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 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(( Ok((
StatusCode::NOT_FOUND, 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] #[tokio::main]
async fn main() -> color_eyre::Result<()> { async fn main() -> color_eyre::Result<()> {
color_eyre::install()?; color_eyre::install()?;
@ -384,15 +401,13 @@ async fn main() -> color_eyre::Result<()> {
sqlx::migrate!().run(&db).await?; sqlx::migrate!().run(&db).await?;
let tera = Tera::new("templates/*.html")?;
tracing::info!("Listening on {addr}"); tracing::info!("Listening on {addr}");
let router = Router::new() let router = Router::new()
.route("/login", get(login)) .route("/login", get(login))
.route("/login/redirect/:id", get(redirected)) .route("/login/redirect/:id", get(redirected))
.fallback(page_not_found) .fallback(page_not_found)
.with_state(Arc::new(AppState { db, oidc, tera })); .with_state(Arc::new(AppState { db, oidc }));
Ok(axum::Server::bind(&addr) Ok(axum::Server::bind(&addr)
.serve(router.into_make_service()) .serve(router.into_make_service())

19
templates/error.html Normal file
View file

@ -0,0 +1,19 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>{{ title }}</title>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bulma@0.9.4/css/bulma.min.css"
/>
</head>
<body>
<section class="section">
<div class="container">
<h1 class="title">Internal Error</h1>
</div>
</section>
</body>
</html>