WIP: refactor: git: use thiserror and cleanup errors
All checks were successful
ci/woodpecker/push/push-next Pipeline was successful
ci/woodpecker/push/cron-docker-builder Pipeline was successful
ci/woodpecker/push/tag-created Pipeline was successful

This commit is contained in:
Paul Campbell 2024-06-03 08:04:48 +01:00
parent 0b8e41a8ec
commit f461d42c99
5 changed files with 47 additions and 58 deletions

View file

@ -41,6 +41,7 @@ secrecy = { workspace = true }
# error handling # error handling
derive_more = { workspace = true } derive_more = { workspace = true }
derive-with = { workspace = true } derive-with = { workspace = true }
thiserror = { workspace = true }
# # file watcher # # file watcher
# inotify = { workspace = true } # inotify = { workspace = true }

View file

@ -4,21 +4,23 @@ use git_next_config as config;
pub type Result<T> = core::result::Result<T, Error>; pub type Result<T> = core::result::Result<T, Error>;
#[derive(Debug, derive_more::Display, derive_more::From)] #[derive(Debug, thiserror::Error)]
pub enum Error { pub enum Error {
Network(kxio::network::NetworkError), #[error("network: {0}")]
Network(#[from] kxio::network::NetworkError),
Fetch(git::fetch::Error), #[error("fetch: {0}")]
Fetch(#[from] git::fetch::Error),
Push(git::push::Error), #[error("push: {0}")]
Push(#[from] git::push::Error),
#[error("lock")]
Lock, Lock,
// Generic(String),
Generic(String), // I1(gix::reference::iter::Error),
I1(gix::reference::iter::Error), // I2(gix::reference::iter::init::Error),
I2(gix::reference::iter::init::Error),
} }
impl std::error::Error for Error {}
pub fn reset( pub fn reset(
repository: &git::OpenRepository, repository: &git::OpenRepository,

View file

@ -1,20 +1,20 @@
#[derive(Debug, derive_more::Display)] #[derive(Debug, thiserror::Error)]
pub enum Error { pub enum Error {
#[error("unable to open repo: {0}")]
UnableToOpenRepo(String), UnableToOpenRepo(String),
#[error("no remote found")]
NoFetchRemoteFound, NoFetchRemoteFound,
#[error("remote connect: {0}")]
Connect(String), Connect(String),
Fetch(String),
#[error("prepare: {0}")]
Prepare(String),
#[error("receive: {0}")]
Receive(String),
#[error("lock")]
Lock, Lock,
} }
impl std::error::Error for Error {}
mod gix_error {
#![cfg(not(tarpaulin_include))] // third-party library errors
use super::Error;
impl From<gix::remote::connect::Error> for Error {
fn from(gix_error: gix::remote::connect::Error) -> Self {
Self::Connect(gix_error.to_string())
}
}
}

View file

@ -14,12 +14,16 @@ impl std::fmt::Display for Force {
} }
} }
#[derive(Debug, derive_more::From, derive_more::Display)] #[derive(Debug, thiserror::Error)]
pub enum Error { pub enum Error {
Open(Box<gix::open::Error>), #[error("gix open: {0}")]
Push, Open(#[from] Box<gix::open::Error>),
#[error("io: {0}")]
Push(#[from] std::io::Error),
#[error("lock")]
Lock, Lock,
} }
impl std::error::Error for Error {}
pub type Result<T> = core::result::Result<T, Error>; pub type Result<T> = core::result::Result<T, Error>;

View file

@ -3,7 +3,7 @@ use crate as git;
use config::BranchName; use config::BranchName;
use git_next_config as config; use git_next_config as config;
use gix::bstr::BStr; use gix::{bstr::BStr, clone::PrepareFetch};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use tracing::{info, warn}; use tracing::{info, warn};
@ -56,17 +56,13 @@ impl super::OpenRepositoryLike for RealOpenRepository {
#[cfg(not(tarpaulin_include))] // test is on local repo - should always have remotes #[cfg(not(tarpaulin_include))] // test is on local repo - should always have remotes
return Err(git::fetch::Error::NoFetchRemoteFound); return Err(git::fetch::Error::NoFetchRemoteFound);
}; };
let prepared_fetch = remote remote
.connect(gix::remote::Direction::Fetch)? .connect(gix::remote::Direction::Fetch)
.prepare_fetch(gix::progress::Discard, Default::default()); .map_err(|gix| git::fetch::Error::Connect(gix.to_string()))?
match prepared_fetch { .prepare_fetch(gix::progress::Discard, Default::default())
Ok(fetch) => { .map_err(|gix| git::fetch::Error::Prepare(gix.to_string()))?
fetch
.receive(gix::progress::Discard, &Default::default()) .receive(gix::progress::Discard, &Default::default())
.map_err(|e| git::fetch::Error::Fetch(e.to_string()))?; .map_err(|gix| git::fetch::Error::Receive(gix.to_string()))?;
}
Err(e) => Err(git::fetch::Error::Fetch(e.to_string()))?,
}
info!("Fetch okay"); info!("Fetch okay");
Ok(()) Ok(())
} }
@ -101,32 +97,18 @@ impl super::OpenRepositoryLike for RealOpenRepository {
.map_err(|_| git::push::Error::Lock) .map_err(|_| git::push::Error::Lock)
.map(|r| r.git_dir().to_path_buf())?; .map(|r| r.git_dir().to_path_buf())?;
let ctx = gix::diff::command::Context { let ctx = gix::diff::command::Context {
git_dir: Some(git_dir.clone()), git_dir: Some(git_dir),
..Default::default() ..Default::default()
}; };
match gix::command::prepare(command.expose_secret()) gix::command::prepare(command.expose_secret())
.with_context(ctx) .with_context(ctx)
.with_shell_allow_argument_splitting() .with_shell_allow_argument_splitting()
.stdout(std::process::Stdio::null()) .stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null()) .stderr(std::process::Stdio::null())
.spawn() .spawn()?
{ .wait()?;
Ok(mut child) => match child.wait() {
Ok(_) => {
info!("Push okay");
Ok(()) Ok(())
} }
Err(err) => {
warn!(?err, ?git_dir, "Failed (wait)");
Err(git::push::Error::Push)
}
},
Err(err) => {
warn!(?err, ?git_dir, "Failed (spawn)");
Err(git::push::Error::Push)
}
}
}
fn commit_log( fn commit_log(
&self, &self,