From 6896a22409943dcf4f01194c780f7d1148b10918 Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Wed, 12 Feb 2020 20:56:47 +0100 Subject: [PATCH] Handle non-existent master branch --- src/error.rs | 20 ++++++++++++++++---- src/main.rs | 7 ++++++- templates/p404_no_master.rs.html | 16 ++++++++++++++++ 3 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 templates/p404_no_master.rs.html diff --git a/src/error.rs b/src/error.rs index 1e6d641..0fd4925 100644 --- a/src/error.rs +++ b/src/error.rs @@ -18,6 +18,7 @@ pub(crate) enum Error { LogBuilder(log4rs::config::Errors), Parse(std::num::ParseIntError), Serial(serde_json::Error), + GitNoMaster, } impl fmt::Display for Error { @@ -32,6 +33,7 @@ impl fmt::Display for Error { Error::LogBuilder(e) => write!(fmt, "LogBuilder({})", e), Error::Parse(e) => write!(fmt, "Parse({})", e), Error::Serial(e) => write!(fmt, "Serial({})", e), + Error::GitNoMaster => write!(fmt, "Repo doesn't have master branch"), } } } @@ -39,10 +41,20 @@ impl fmt::Display for Error { impl ResponseError for Error { fn error_response(&self) -> HttpResponse { let mut buf = Vec::new(); - templates::p500(&mut buf, VERSION_INFO, REPO_COUNT.load(Ordering::Relaxed)).unwrap(); - HttpResponse::InternalServerError() - .content_type("text/html") - .body(buf) + match self { + Error::GitNoMaster => { + templates::p404_no_master(&mut buf, VERSION_INFO, REPO_COUNT.load(Ordering::Relaxed)).unwrap(); + HttpResponse::NotFound() + .content_type("text/html") + .body(buf) + }, + _ => { + templates::p500(&mut buf, VERSION_INFO, REPO_COUNT.load(Ordering::Relaxed)).unwrap(); + HttpResponse::InternalServerError() + .content_type("text/html") + .body(buf) + } + } } } diff --git a/src/main.rs b/src/main.rs index 620377d..bd69145 100644 --- a/src/main.rs +++ b/src/main.rs @@ -75,7 +75,12 @@ fn hoc(repo: &str, repo_dir: &str, cache_dir: &str) -> Result<(u64, String, u64) let cache_dir = format!("{}/{}.json", cache_dir, repo); let cache_dir = Path::new(&cache_dir); let repo = Repository::open_bare(&repo_dir)?; - let head = format!("{}", repo.head()?.target().ok_or(Error::Internal)?); + // TODO: do better... + let head = match repo.head() { + Ok(v) => v, + Err(_) => return Err(Error::GitNoMaster), + }; + let head = format!("{}", head.target().ok_or(Error::Internal)?); let mut arg_commit_count = vec!["rev-list".to_string(), "--count".to_string()]; let mut arg = vec![ "log".to_string(), diff --git a/templates/p404_no_master.rs.html b/templates/p404_no_master.rs.html new file mode 100644 index 0000000..dd49d8a --- /dev/null +++ b/templates/p404_no_master.rs.html @@ -0,0 +1,16 @@ +@use super::base; +@use crate::statics::VersionInfo; + +@(version_info: VersionInfo, repo_count: usize) + +@:base("Master Branch not Found - Hits-of-Code Badges", "404 - Master Branch not Found", { +

+Sorry. I couldn't find the master branch of your repositroy. +Currently this service depends on the existence of a master branch. Please go +back to the homepage. +

+ +

+If you think, this is a mistake on my side, please drop me a mail. +

+}, version_info, repo_count)