mygit

[UNMAINTAINED] A cgit/webgit alternative, written in Rust
Log | Files | Refs | README | LICENSE

commit 88c1c9775c211e2cb4d8c54680f687c9617ed0cb
parent 993a4e109bcba030251e42e4595e1044e13cfb3d
Author: Johann150 <johann@qwertqwefsday.eu>
Date:   Sun, 21 Mar 2021 11:52:37 +0100

add error page

Diffstat:
MCargo.lock | 1+
MCargo.toml | 1+
Asrc/errorpage.rs | 31+++++++++++++++++++++++++++++++
Msrc/main.rs | 13++++++++++---
Atemplates/error.html | 9+++++++++
5 files changed, 52 insertions(+), 3 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock @@ -1167,6 +1167,7 @@ dependencies = [ "askama", "askama_tide", "async-std", + "async-trait", "chrono", "git2", "once_cell", diff --git a/Cargo.toml b/Cargo.toml @@ -12,6 +12,7 @@ anyhow = "1.0" askama = {version = "0.10", features = ["with-tide"]} askama_tide = "0.13" async-std = { version = "1.8.0", features = ["attributes"] } +async-trait = "0.1.48" chrono = "0.4" git2 = {version="0.13", default-features = false} once_cell = "1.7.2" diff --git a/src/errorpage.rs b/src/errorpage.rs @@ -0,0 +1,31 @@ +use askama::Template; +use tide::{Middleware, Next, Request, StatusCode}; + +#[derive(Template)] +#[template(path = "error.html")] +struct ErrorTemplate { + resource: String, + status: StatusCode, + message: String, +} + +pub struct ErrorToErrorpage; + +#[async_trait::async_trait] +impl<State: Clone + Send + Sync + 'static> Middleware<State> for ErrorToErrorpage{ + async fn handle(&self, req: Request<State>, next: Next<'_, State>) -> tide::Result { + let resource = req.url().path().to_string(); + let mut response = next.run(req).await; + if let Some(err) = response.take_error() { + let status = err.status(); + response = ErrorTemplate{ + resource, + status, + message: err.into_inner().to_string(), + }.into(); + response.set_status(status); + } + + Ok(response) + } +} diff --git a/src/main.rs b/src/main.rs @@ -12,6 +12,8 @@ use syntect::highlighting::ThemeSet; use syntect::parsing::SyntaxSet; use tide::Request; +mod errorpage; + #[derive(Deserialize, Debug)] pub struct Config { #[serde(default = "defaults::port")] @@ -129,14 +131,18 @@ struct RepoHomeTemplate { readme_text: String, } -fn repo_from_request(repo_name: &str) -> Result<Repository> { +fn repo_from_request(repo_name: &str) -> Result<Repository, tide::Error> { let repo_name = percent_encoding::percent_decode_str(repo_name) .decode_utf8_lossy() .into_owned(); let repo_path = Path::new(&CONFIG.projectroot).join(repo_name); // TODO CLEAN PATH! VERY IMPORTANT! DONT FORGET! - let r = Repository::open(repo_path)?; - Ok(r) + Repository::open(repo_path).or_else(|_| { + Err(tide::Error::from_str( + 404, + "This repository does not exist.", + )) + }) } async fn repo_home(req: Request<()>) -> tide::Result { @@ -468,6 +474,7 @@ mod filters { async fn main() -> Result<(), std::io::Error> { tide::log::start(); let mut app = tide::new(); + app.with(errorpage::ErrorToErrorpage); app.at("/").get(index); app.at("/robots.txt") .serve_file("templates/static/robots.txt")?; // TODO configurable diff --git a/templates/error.html b/templates/error.html @@ -0,0 +1,9 @@ +{% extends "base.html" %} + +{% block content %} + <div class="page-title"><h1>Hmm, that did not work.</h1></div> + <div> + Something went wrong in getting "{{resource}}":<br> + {{status}} &mdash; {{message}} + </div> +{% endblock %}