Adds more error handling
Build Successful The build was successful
Gitea/epoch_forge/pipeline/head This commit looks good
Details
Build Successful The build was successful
Gitea/epoch_forge/pipeline/head This commit looks good
Details
This commit is contained in:
parent
3e309291e8
commit
ade58880e3
|
|
@ -24,6 +24,7 @@ pub fn handle_cmd(cmd: cli_epoch::EpochCmd) -> Result<(), AppError> {
|
|||
cli_epoch::EpochCmd::Reset(args) => handle_reset(args, &mut main_epoch),
|
||||
cli_epoch::EpochCmd::Diff(args) => handle_diff(args),
|
||||
cli_epoch::EpochCmd::Tag(args) => handle_taging(args, &mut main_epoch),
|
||||
cli_epoch::EpochCmd::Fsck(args) => handle_fsck(args, &mut main_epoch),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -111,3 +112,10 @@ fn handle_taging(args: cli_epoch::Tag, main_epoch: &mut EpochConfig) -> Result<(
|
|||
)))
|
||||
}
|
||||
}
|
||||
|
||||
/// Basic sanity check
|
||||
fn handle_fsck(args: cli_epoch::Fsck, main_epoch: &mut EpochConfig) -> Result<(), AppError> {
|
||||
let _ = main_epoch;
|
||||
let _ = args;
|
||||
unimplemented!()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,6 +21,9 @@ pub enum EpochCmd {
|
|||
// Tag and next could be implemented as subcommands of epoch
|
||||
// even if they barely belong here
|
||||
Tag(Tag),
|
||||
|
||||
/// Basic filesystem level maintenance
|
||||
Fsck(Fsck),
|
||||
}
|
||||
|
||||
/// `efo epoch log`
|
||||
|
|
@ -69,3 +72,7 @@ pub struct Tag {
|
|||
/// The tag to tag the current epoch with
|
||||
pub tag: String,
|
||||
}
|
||||
|
||||
/// `efo epoch fsck`
|
||||
#[derive(Debug, Args)]
|
||||
pub struct Fsck {}
|
||||
|
|
|
|||
|
|
@ -81,7 +81,8 @@ impl EpochConfig {
|
|||
// TODO: write a trait for this
|
||||
let config: EpochConfig = serde_json::from_str(&io_err_with_help!(
|
||||
read_to_string(path),
|
||||
format!("Reading config from path {:?}", path)
|
||||
"Reading config from path {:?}",
|
||||
path
|
||||
)?)
|
||||
.map_err(|e| EpochError::Serde {
|
||||
source: e,
|
||||
|
|
@ -99,19 +100,11 @@ impl EpochConfig {
|
|||
/// Initialize the epoch more in depth
|
||||
fn initialize(mut self) -> Result<Self, EpochError> {
|
||||
let path = EpochConfig::default().save_subfolder();
|
||||
let path = io_err_with_help!(
|
||||
Path::new(&path).read_dir(),
|
||||
format!("Reading initial folder {path}")
|
||||
)?;
|
||||
let path = io_err_with_help!(Path::new(&path).read_dir(), "Reading initial folder {path}")?;
|
||||
// let mut valid_folders = HashMap::new();
|
||||
for elt in path {
|
||||
let elt = io_err_with_help!(elt, "Opening file in main config folder")?;
|
||||
if io_err_with_help!(
|
||||
elt.file_type(),
|
||||
format!("Determining file type of {:?}", elt)
|
||||
)?
|
||||
.is_dir()
|
||||
{
|
||||
if io_err_with_help!(elt.file_type(), "Determining file type of {:?}", elt)?.is_dir() {
|
||||
let id = elt.file_name().to_string_lossy().parse::<EpochId>();
|
||||
println!("{:?}", id);
|
||||
self.list_existing_epochs.insert(id.unwrap(), None);
|
||||
|
|
@ -127,8 +120,7 @@ impl EpochConfig {
|
|||
|
||||
// Create a mock epoch
|
||||
let path = EpochInfo::with_id(epoch_id).save_path();
|
||||
let read_to_string =
|
||||
io_err_with_help!(read_to_string(&path), format!("Reading {:?}", path));
|
||||
let read_to_string = io_err_with_help!(read_to_string(&path), "Reading {:?}", path);
|
||||
|
||||
// Try to load
|
||||
let maybe_epoch: EpochInfo =
|
||||
|
|
@ -177,7 +169,8 @@ impl EpochConfig {
|
|||
}
|
||||
io_err_with_help!(
|
||||
remove_dir_all(EpochInfo::with_id(&id).save_folder()),
|
||||
format!("Removing the epoch {}", id)
|
||||
"Removing the epoch {}",
|
||||
id
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -163,6 +163,10 @@ pub enum EpochError {
|
|||
/// Any other error that didn't fit into the above categories
|
||||
#[error("An unknown error occurred: {0}")]
|
||||
Unknown(String),
|
||||
|
||||
/// User error, meant to be very explicit over what happened and what to do
|
||||
#[error("An user facing error")]
|
||||
UserError(#[help] String),
|
||||
}
|
||||
|
||||
// macro_rules! err_with {
|
||||
|
|
@ -175,10 +179,10 @@ pub enum EpochError {
|
|||
macro_rules! io_err_with_help {
|
||||
// $expr: the expression that produces the Result
|
||||
// $help: string literal with the help message
|
||||
($expr:expr, $help:expr) => {{
|
||||
($expr:expr, $($help:expr),*) => {{
|
||||
$expr.map_err(|e| EpochError::Io {
|
||||
source: e,
|
||||
help: Some($help.to_owned()),
|
||||
help: Some(format!($($help),*)),
|
||||
})
|
||||
}};
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue