Handle non-existent master branch
This commit is contained in:
parent
3c2f06ebae
commit
6896a22409
20
src/error.rs
20
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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(),
|
||||
|
16
templates/p404_no_master.rs.html
Normal file
16
templates/p404_no_master.rs.html
Normal 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)
|
Loading…
Reference in New Issue
Block a user