feat: report 404 when file not found in Git repo

This commit is contained in:
Aravinth Manivannan 2022-11-12 14:27:05 +05:30
parent 3a961bc524
commit 3c3ff0f8a7
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
2 changed files with 20 additions and 5 deletions

View file

@ -86,6 +86,10 @@ pub enum ServiceError {
/// website not found
WebsiteNotFound,
#[display(fmt = "File not found")]
/// File not found
FileNotFound,
/// when the a path configured for a page is already taken
#[display(
fmt = "Path already used for another website. lhs: {:?} rhs: {:?}",
@ -236,6 +240,7 @@ impl ResponseError for ServiceError {
ServiceError::EmailTaken => StatusCode::BAD_REQUEST,
ServiceError::UsernameTaken => StatusCode::BAD_REQUEST,
ServiceError::AccountNotFound => StatusCode::NOT_FOUND,
ServiceError::FileNotFound => StatusCode::NOT_FOUND,
ServiceError::ProfanityError => StatusCode::BAD_REQUEST, //BADREQUEST,
ServiceError::BlacklistError => StatusCode::BAD_REQUEST, //BADREQUEST,

View file

@ -173,8 +173,6 @@ fn read_file_inner(
}
let inner = |repo: &git2::Repository, tree: &git2::Tree| -> ServiceResult<FileInfo> {
// let head = repo.head().unwrap();
// let tree = head.peel_to_tree().unwrap();
let mut path = path;
if path == "/" {
let content = get_index_file(tree.id(), repo);
@ -187,8 +185,16 @@ fn read_file_inner(
if path.starts_with('/') {
path = path.trim_start_matches('/');
}
let entry = tree.get_path(Path::new(path)).unwrap();
//FileType::Dir(items)
fn file_not_found(e: git2::Error) -> ServiceError {
if e.code() == ErrorCode::NotFound {
if e.class() == ErrorClass::Tree {
return ServiceError::FileNotFound;
}
}
return e.into();
}
let entry = tree.get_path(Path::new(path)).map_err(file_not_found)?;
let mode: GitFileMode = entry.clone().into();
if let Some(name) = entry.name() {
@ -212,7 +218,6 @@ fn read_file_inner(
}
};
//let repo = git2::Repository::open(repo_path).unwrap();
inner(repo, tree)
}
@ -289,5 +294,10 @@ pub mod tests {
assert_eq!(resp.filename, "README.txt");
assert_eq!(resp.content.bytes(), FILE_CONTENT.as_bytes());
assert_eq!(resp.mime.first().unwrap(), "text/plain");
assert_eq!(
read_preview_file(&Path::new(PATH).into(), "master", "file-does-not-exist.txt"),
Err(ServiceError::FileNotFound)
);
}
}