Handle non-existent master branch

This commit is contained in:
Valentin Brandl 2020-02-12 20:56:47 +01:00
parent 3c2f06ebae
commit 6896a22409
No known key found for this signature in database
GPG Key ID: 30D341DD34118D7D
3 changed files with 38 additions and 5 deletions

View File

@ -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,12 +41,22 @@ impl fmt::Display for Error {
impl ResponseError for Error {
fn error_response(&self) -> HttpResponse {
let mut buf = Vec::new();
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)
}
}
}
}
impl std::error::Error for Error {}

View File

@ -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(),

View File

@ -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", {
<p>
<big>Sorry</big>. I couldn't find the master branch of your repositroy.
Currently this service depends on the existence of a master branch. Please go
<a href="/">back to the homepage</a>.
</p>
<p>
If you think, this is a mistake on my side, please <a href="mailto:mail+hoc@@vbrandl.net">drop me a mail</a>.
</p>
}, version_info, repo_count)