3fc191ea

Archive
Tree [3fc191ea] [>]
commit
3fc191eacf4e5f683fea4ab61048250d22bf62f2
parent
author
Christopher K. Schmitt <me@shmish.dev>
date
2026-07-29
committer
Christopher K. Schmitt <me@shmish.dev>
date
2026-07-29
changes
2
insertions
51
deletions
6
Add styled error pages
Msrc/error.rs
-use axum::http::StatusCode;+use askama::Template;+use axum::http::{StatusCode, header};~use axum::response::{IntoResponse, Response};~use thiserror::Error;~
~~pub type Result<T> = std::result::Result<T, Error>;~+#[derive(Template)]+#[template(path = "error.html")]+struct ErrorTemplate {+    status: u16,+    kind: &'static str,+    message: String,+    base_url: String,+}+~impl IntoResponse for Error {~    fn into_response(self) -> Response {-        let status = match &self {-            Error::RepoNotFound(_) | Error::NotFound(_) => StatusCode::NOT_FOUND,-            Error::BadRequest(_) => StatusCode::BAD_REQUEST,-            Error::Io(_) | Error::GitCorrupt(_) => StatusCode::INTERNAL_SERVER_ERROR,+        let (status, kind) = match &self {+            Error::RepoNotFound(_) | Error::NotFound(_) => (StatusCode::NOT_FOUND, "Not Found"),+            Error::BadRequest(_) => (StatusCode::BAD_REQUEST, "Bad Request"),+            Error::Io(_) | Error::GitCorrupt(_) => {+                (StatusCode::INTERNAL_SERVER_ERROR, "Internal Server Error")+            }~        };-        (status, self.to_string()).into_response()++        let tpl = ErrorTemplate {+            status: status.as_u16(),+            kind,+            message: self.to_string(),+            base_url: crate::base_url().to_string(),+        };++        match tpl.render() {+            Ok(html) => ([(header::CONTENT_TYPE, "text/html; charset=utf-8")], html).into_response(),+            Err(_) => (status, self.to_string()).into_response(),+        }~    }~}
Atemplates/error.html
+<!DOCTYPE html>+<html lang="en">+<head>+    <meta charset="UTF-8">+    <meta name="viewport" content="width=device-width, initial-scale=1.0">+    <title>{{ status }} - {{ kind }}</title>+    <link rel="stylesheet" href="{{ base_url }}/style.css">+</head>+<body>+    <header>+        <ul>+            <li><a href="{{ base_url }}/">home</a></li>+            <li>error</li>+        </ul>+    </header>+    <main>+        <h1>{{ status }} {{ kind }}</h1>+        <p>{{ message }}</p>+    </main>+    <footer>served with <a href="#">{{ crate::PKG_NAME }}</a> v{{ crate::PKG_VERSION }}</footer>+</body>+</html>