Use custom error handling for repo opening
This commit is contained in:
34
src/repo.rs
34
src/repo.rs
@@ -12,6 +12,31 @@ pub enum RemoteType {
|
|||||||
Https,
|
Https,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
pub enum RepoErrorKind {
|
||||||
|
NotFound,
|
||||||
|
Unknown(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct RepoError {
|
||||||
|
pub kind: RepoErrorKind,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RepoError {
|
||||||
|
fn new(kind: RepoErrorKind) -> RepoError {
|
||||||
|
RepoError { kind }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for RepoError {}
|
||||||
|
|
||||||
|
impl std::fmt::Display for RepoError {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
|
write!(f, "{:?}", self.kind)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
#[serde(deny_unknown_fields)]
|
#[serde(deny_unknown_fields)]
|
||||||
pub struct Remote {
|
pub struct Remote {
|
||||||
@@ -104,10 +129,15 @@ pub fn detect_remote_type(remote_url: &str) -> Option<RemoteType> {
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn open_repo(path: &Path) -> Result<Repository, Box<dyn std::error::Error>> {
|
pub fn open_repo(path: &Path) -> Result<Repository, RepoError> {
|
||||||
match Repository::open(path) {
|
match Repository::open(path) {
|
||||||
Ok(r) => Ok(r),
|
Ok(r) => Ok(r),
|
||||||
Err(e) => Err(Box::new(e)),
|
Err(e) => match e.code() {
|
||||||
|
git2::ErrorCode::NotFound => Err(RepoError::new(RepoErrorKind::NotFound)),
|
||||||
|
_ => Err(RepoError::new(RepoErrorKind::Unknown(
|
||||||
|
e.message().to_string(),
|
||||||
|
))),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user