error: Add more error types

Since error refactoring was quite incomplete, I've added more errors for other parts of Vento. This should cover almost every error except slot errors, since those require their own formatting.
This commit is contained in:
Lux Aliaga 2023-02-19 14:42:26 -03:00
parent 67d2da8767
commit 27b42e963e
Signed by: lux
GPG Key ID: B56C805968637437
6 changed files with 42 additions and 64 deletions

35
Cargo.lock generated
View File

@ -2,12 +2,6 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "adler"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "ahash"
version = "0.7.6"
@ -125,15 +119,6 @@ dependencies = [
"libc",
]
[[package]]
name = "crc32fast"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d"
dependencies = [
"cfg-if",
]
[[package]]
name = "crypto-common"
version = "0.1.6"
@ -192,16 +177,6 @@ dependencies = [
"windows-sys",
]
[[package]]
name = "flate2"
version = "1.0.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8a2db397cb1c8772f31494cb8917e48cd1e64f0fa7efac59fbd741a0a8ce841"
dependencies = [
"crc32fast",
"miniz_oxide",
]
[[package]]
name = "fs_extra"
version = "1.2.0"
@ -323,15 +298,6 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
[[package]]
name = "miniz_oxide"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b275950c28b37e794e8c55d88aeb5e139d0ce23fdbbeda68f8d7174abdf9e8fa"
dependencies = [
"adler",
]
[[package]]
name = "nom"
version = "7.1.1"
@ -678,7 +644,6 @@ dependencies = [
"colored",
"config",
"dirs",
"flate2",
"fs_extra",
"man",
"size_format",

View File

@ -18,8 +18,7 @@
*/
use crate::error::{throw_error, ErrorType};
use anyhow::{bail, Result};
use colored::Colorize;
use anyhow::Result;
use config::Config;
use std::env::current_dir;
use std::fs::File;
@ -52,7 +51,7 @@ pub fn env_config() -> Result<Settings> {
_ => PathBuf::new(),
};
if home == PathBuf::new() {
bail!("{}", "Vento was unable to detect your home folder. Have you configured your environment correctly?".red());
throw_error(ErrorType::NoHomeDirectory)?;
};
let custom_dir = Path::new(&dir_config()?).to_path_buf();
let vento_dir: PathBuf = if custom_dir != PathBuf::new() {

View File

@ -25,6 +25,14 @@ pub enum ErrorType {
SpecifySlot,
SpecifyFile,
NoCurrentDirectory,
NoHomeDirectory,
InvalidHistoryLength,
IllegalAction,
NotInitialized,
NoAccessParent,
ExistsInventory,
ExistsDestination,
NoFileOrDir,
}
pub fn throw_error(error: ErrorType) -> Result<()> {
@ -35,6 +43,14 @@ pub fn throw_error(error: ErrorType) -> Result<()> {
ErrorType::SpecifyFile => "You need to specify a file",
ErrorType::SpecifySlot => "You need to specify a slot",
ErrorType::NoCurrentDirectory => "Vento was unable to detect your current directory. Have you configured your environment correctly?",
ErrorType::NoHomeDirectory => "Vento was unable to detect your home directory. Have you configured your environment correctly?",
ErrorType::InvalidHistoryLength => "Invalid history length",
ErrorType::IllegalAction => "Illegal action",
ErrorType::NotInitialized => "Vento not initialized. Run \"vento -i\" to initialize Vento",
ErrorType::NoAccessParent => "Cannot access parent",
ErrorType::ExistsInventory => "A file with the same name already exists in your inventory!",
ErrorType::ExistsDestination => "A file with the same name already exists in the destination! Try renaming it or dropping this file somewhere else",
ErrorType::NoFileOrDir => "No such file or directory",
}
.red()
);

View File

@ -17,8 +17,12 @@
*
*/
use crate::{common, inv, item};
use anyhow::{bail, Result};
use crate::{
common,
error::{throw_error, ErrorType},
inv, item,
};
use anyhow::Result;
use colored::Colorize;
use std::fs;
use std::path::{Path, PathBuf};
@ -41,7 +45,7 @@ pub fn undo() -> Result<()> {
}
if contents.len() != 4 {
bail!("Invalid history length".red());
throw_error(ErrorType::InvalidHistoryLength)?;
}
match contents[3] {
@ -56,7 +60,7 @@ pub fn undo() -> Result<()> {
"switch" => {
inv::switch(false)?;
}
_ => bail!("Illegal action".red()),
_ => throw_error(ErrorType::IllegalAction)?,
}
println!(

View File

@ -17,7 +17,10 @@
*
*/
use super::common;
use super::{
common,
error::{throw_error, ErrorType},
};
use anyhow::{bail, Context, Result};
use colored::Colorize;
use size_format::SizeFormatterBinary;
@ -51,10 +54,7 @@ pub fn list(slot: &str, dir: &str) -> Result<()> {
if !ventodir.is_dir() {
// Detects if Vento hasn't been initialized and bails if so
bail!(
"{}",
"Vento not initialized. Run \"vento -i\" to initialize Vento".red()
);
throw_error(ErrorType::NotInitialized)?;
}
let mut slotdir: PathBuf = match slot {
@ -70,7 +70,7 @@ pub fn list(slot: &str, dir: &str) -> Result<()> {
if dir.to_string().contains("..") {
// Basically preventing from listing anything out of bounds. ls and dir exist for that
bail!("{}", "Cannot access parent".red());
throw_error(ErrorType::NoAccessParent)?;
}
if !slotdir.is_dir() {

View File

@ -17,7 +17,10 @@
*
*/
use super::common;
use super::{
common,
error::{throw_error, ErrorType},
};
use anyhow::{bail, Result};
use colored::Colorize;
use fs_extra::dir::{move_dir, CopyOptions};
@ -30,10 +33,7 @@ pub fn take(file: &String, slot: &str, message: bool) -> Result<()> {
if !ventodir.is_dir() {
// Detects if Vento hasn't been initialized and bails if so
bail!(
"{}",
"Vento not initialized. Run \"vento -i\" to initialize Vento".red()
);
throw_error(ErrorType::NotInitialized)?;
};
let slotdir: PathBuf = match slot {
"active" | "a" => common::env_config()?.active_dir,
@ -64,10 +64,7 @@ pub fn take(file: &String, slot: &str, message: bool) -> Result<()> {
if Path::exists(&destpath) {
// Checks if there's a file with the same name in the inventory.
bail!(
"{}",
"A file with the same name already exists in your inventory!".red()
);
throw_error(ErrorType::ExistsInventory)?;
}
if sourcepath.is_file() | sourcepath.is_symlink() {
@ -78,7 +75,7 @@ pub fn take(file: &String, slot: &str, message: bool) -> Result<()> {
let options = CopyOptions::new();
move_dir(file, &slotdir, &options)?;
} else {
bail!("{}", "No such file or directory".red());
throw_error(ErrorType::NoFileOrDir)?;
}
common::history(common::HistoryData {
@ -116,10 +113,7 @@ pub fn drop(file: &String, slot: &str, dest: PathBuf, message: bool) -> Result<(
if !ventodir.is_dir() {
// Detects if Vento hasn't been initialized and bails if so
bail!(
"{}",
"Vento not initialized. Run \"vento -i\" to initialize Vento".red()
);
throw_error(ErrorType::NotInitialized)?;
};
let slotdir: PathBuf = match slot {
@ -151,7 +145,7 @@ pub fn drop(file: &String, slot: &str, dest: PathBuf, message: bool) -> Result<(
if Path::exists(&destpath) {
// Checks if there's a file with the same name in the destination path.
bail!("{}", "A file with the same name already exists in the destination! Try renaming it or dropping this file somewhere else".red());
throw_error(ErrorType::ExistsDestination)?;
}
if sourcepath.is_file() | sourcepath.is_symlink() {
@ -163,7 +157,7 @@ pub fn drop(file: &String, slot: &str, dest: PathBuf, message: bool) -> Result<(
let options = CopyOptions::new();
move_dir(&sourcepath, destpath, &options)?;
} else {
bail!("{}", "No such file or directory".red());
throw_error(ErrorType::NoFileOrDir)?;
}
destpath.pop();