diff --git a/src/errors.rs b/src/errors.rs index 3a07299..450fefa 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -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, diff --git a/src/git.rs b/src/git.rs index 9416ba4..316a9a4 100644 --- a/src/git.rs +++ b/src/git.rs @@ -173,8 +173,6 @@ fn read_file_inner( } let inner = |repo: &git2::Repository, tree: &git2::Tree| -> ServiceResult { - // 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) + ); } }