Skip to content

Commit

Permalink
Issue #109 | Improving errors (#184)
Browse files Browse the repository at this point in the history
* New error type

A new error type has been created for the binary part of the tool,
and the project should compile and work. It works with printing out
the errors, and works just like any good old Rust error. There is
however one weak point, and that is debugging is hard with this method.
This should be solved before this is merged into main.

* Minor edit

* Update src/bin/huak/main.rs

Co-authored-by: Chris Pryer <[email protected]>

* Update src/bin/huak/main.rs

Co-authored-by: Chris Pryer <[email protected]>

* Changed from CliErrorCode to CliErrorType, prints out anyhow errors now,
and the option for Ruff, PyBlack and PyTest errors can be done. Fixed .gitignore too.

* Update src/huak/errors.rs

Co-authored-by: Chris Pryer <[email protected]>

* Fixed main.rs after merging with the updated main branch.

* Formatting

* Making the linter happy

Co-authored-by: Chris Pryer <[email protected]>
  • Loading branch information
Adrian Berg and cnpryer authored Sep 23, 2022
1 parent faaf1bf commit 8a03059
Show file tree
Hide file tree
Showing 30 changed files with 124 additions and 105 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,3 @@ venv.bak/

# Misc.
.DS_Store


2 changes: 1 addition & 1 deletion src/bin/huak/commands/activate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ pub fn cmd() -> Command<'static> {
}

/// Run the `activate` command.
pub fn run() -> CliResult {
pub fn run() -> CliResult<()> {
unimplemented!()
}
2 changes: 1 addition & 1 deletion src/bin/huak/commands/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn cmd() -> Command<'static> {
}

/// Run the `add` subcommand.
pub fn run(args: &ArgMatches) -> CliResult {
pub fn run(args: &ArgMatches) -> CliResult<()> {
let _ = args.get_one::<String>("dependency");

unimplemented!()
Expand Down
2 changes: 1 addition & 1 deletion src/bin/huak/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ pub fn cmd() -> Command<'static> {
}

/// Run the `build` command.
pub fn run() -> CliResult {
pub fn run() -> CliResult<()> {
unimplemented!()
}
2 changes: 1 addition & 1 deletion src/bin/huak/commands/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn cmd() -> Command<'static> {
}

/// Run the `clean` command.
pub fn run() -> CliResult {
pub fn run() -> CliResult<()> {
let cwd = env::current_dir()?;
let project = Project::from(cwd)?;

Expand Down
17 changes: 7 additions & 10 deletions src/bin/huak/commands/clean_pycache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::utils::subcommand;
use anyhow::Error;
use clap::Command;
use glob::{glob, Paths, PatternError};
use huak::errors::{CliError, CliResult};
use huak::errors::{CliError, CliErrorType, CliResult};
use std::fs::{remove_dir_all, remove_file};

#[derive(Clone, Copy)]
Expand All @@ -20,10 +20,10 @@ pub fn cmd() -> Command<'static> {
.about("Remove all .pyc files and __pycache__ directories.")
}

pub fn run() -> CliResult {
pub fn run() -> CliResult<()> {
let mut success: bool = true;

let mut error: Option<Error> = None;
let mut _error: Option<Error> = None;
for i in get_delete_patterns() {
let files: Result<Paths, PatternError> = glob(&i.glob);

Expand All @@ -39,21 +39,21 @@ pub fn run() -> CliResult {
Ok(_) => (),
Err(e) => {
file_level_success = false;
error = Some(Error::new(e));
_error = Some(Error::new(e));
}
}
}
PathType::File => match remove_file(p) {
Ok(_) => (),
Err(e) => {
file_level_success = false;
error = Some(Error::new(e));
_error = Some(Error::new(e));
}
},
},
Err(e) => {
file_level_success = false;
error = Some(Error::new(e))
_error = Some(Error::new(e))
}
}
}
Expand All @@ -69,10 +69,7 @@ pub fn run() -> CliResult {
if success {
Ok(())
} else {
Err(CliError {
error,
exit_code: 2,
})
Err(CliError::new(CliErrorType::IOError))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/bin/huak/commands/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn cmd() -> Command<'static> {
}

/// Run the `doc` command.
pub fn run(args: &ArgMatches) -> CliResult {
pub fn run(args: &ArgMatches) -> CliResult<()> {
// TODO: Use is_check.
let _ = args.get_one::<bool>("check").unwrap();

Expand Down
2 changes: 1 addition & 1 deletion src/bin/huak/commands/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn cmd() -> Command<'static> {
}

/// Run the `fmt` command.
pub fn run(args: &ArgMatches) -> CliResult {
pub fn run(args: &ArgMatches) -> CliResult<()> {
// This command runs from the context of the cwd.
let cwd = env::current_dir()?;
let project = Project::from(cwd)?;
Expand Down
2 changes: 1 addition & 1 deletion src/bin/huak/commands/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ pub fn cmd() -> Command<'static> {
}

/// Run the `help` command.
pub fn run() -> CliResult {
pub fn run() -> CliResult<()> {
unimplemented!()
}
2 changes: 1 addition & 1 deletion src/bin/huak/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn cmd() -> Command<'static> {
}

/// Run the `init` command.
pub fn run() -> CliResult {
pub fn run() -> CliResult<()> {
let cwd = env::current_dir()?;

let project = Project::from(cwd)?;
Expand Down
2 changes: 1 addition & 1 deletion src/bin/huak/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn cmd() -> Command<'static> {
}

/// Run the `install` command.
pub fn run() -> CliResult {
pub fn run() -> CliResult<()> {
let cwd = env::current_dir()?;
let project = Project::from(cwd)?;

Expand Down
2 changes: 1 addition & 1 deletion src/bin/huak/commands/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn cmd() -> Command<'static> {
}

/// Run the `lint` command.
pub fn run() -> CliResult {
pub fn run() -> CliResult<()> {
// This command runs from the context of the cwd.
let cwd = env::current_dir()?;
let project = Project::from(cwd)?;
Expand Down
14 changes: 4 additions & 10 deletions src/bin/huak/commands/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fs;

use super::utils::subcommand;
use clap::{arg, value_parser, ArgMatches, Command};
use huak::errors::{CliError, CliResult};
use huak::errors::{CliError, CliErrorType, CliResult};
use huak::ops;
use huak::project::Project;

Expand All @@ -16,7 +16,7 @@ pub fn cmd() -> Command<'static> {

/// Run the `new` command.
// TODO: Ops should hanlde the path creation step in addition to the project creation.
pub fn run(args: &ArgMatches) -> CliResult {
pub fn run(args: &ArgMatches) -> CliResult<()> {
// This command runs from the current working directory
// Each command's behavior is triggered from the context of the cwd.
let cwd = env::current_dir()?;
Expand All @@ -29,18 +29,12 @@ pub fn run(args: &ArgMatches) -> CliResult {

// Make sure there isn't already a path we would override.
if path.exists() && path != cwd {
return Err(CliError::new(
anyhow::format_err!("a directory already exists"),
2,
));
return Err(CliError::new(CliErrorType::DirectoryExists));
}

// If the current directory is used it must be empty. User should use init.
if path == cwd && path.read_dir()?.count() > 0 {
return Err(CliError::new(
anyhow::format_err!("cwd was used but isn't empty"),
2,
));
return Err(CliError::new(CliErrorType::DirectoryExists));
}

// Create project directory.
Expand Down
2 changes: 1 addition & 1 deletion src/bin/huak/commands/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ pub fn cmd() -> Command<'static> {
}

/// Run the `publish` command.
pub fn run() -> CliResult {
pub fn run() -> CliResult<()> {
unimplemented!()
}
10 changes: 3 additions & 7 deletions src/bin/huak/commands/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::env;

use super::utils::subcommand;
use clap::{value_parser, Arg, ArgMatches, Command};
use huak::errors::CliErrorType;
use huak::ops;
use huak::{
errors::{CliError, CliResult},
Expand All @@ -20,15 +21,10 @@ pub fn cmd() -> Command<'static> {
}

/// Run the `remove` command.
pub fn run(args: &ArgMatches) -> CliResult {
pub fn run(args: &ArgMatches) -> CliResult<()> {
let dependency = match args.get_one::<String>("dependency") {
Some(d) => d,
None => {
return Err(CliError::new(
anyhow::format_err!("no dependency was provided"),
2,
))
}
None => return Err(CliError::new(CliErrorType::MissingArguments)),
};
let cwd = env::current_dir()?;
let project = Project::from(cwd)?;
Expand Down
2 changes: 1 addition & 1 deletion src/bin/huak/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn cmd() -> Command<'static> {
}

/// Run the `run` command.
pub fn run(args: &ArgMatches) -> CliResult {
pub fn run(args: &ArgMatches) -> CliResult<()> {
let _ = args.get_many::<String>("command");

unimplemented!()
Expand Down
2 changes: 1 addition & 1 deletion src/bin/huak/commands/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn cmd() -> Command<'static> {

/// Run the `test` command.
// TODO: Use pyproject.toml for configuration overrides.
pub fn run() -> CliResult {
pub fn run() -> CliResult<()> {
// This command runs from the context of the cwd.
let cwd = env::current_dir()?;
let project = Project::from(cwd)?;
Expand Down
2 changes: 1 addition & 1 deletion src/bin/huak/commands/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn cmd() -> Command<'static> {
}

/// Run the `update` command.
pub fn run(args: &ArgMatches) -> CliResult {
pub fn run(args: &ArgMatches) -> CliResult<()> {
let _ = args.get_one::<String>("dependency").unwrap();

unimplemented!()
Expand Down
2 changes: 1 addition & 1 deletion src/bin/huak/commands/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn cmd() -> Command<'static> {
}

/// Run the `version` command.
pub fn run() -> CliResult {
pub fn run() -> CliResult<()> {
let cwd = env::current_dir()?;
let project = Project::from(cwd)?;

Expand Down
19 changes: 10 additions & 9 deletions src/bin/huak/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@
//!
//! Huak implements a cli application with various subcommands.
use clap::{self, AppSettings, ArgMatches};
use huak::errors::{CliError, CliResult};
use huak::errors::{CliError, CliErrorType, CliResult};

mod commands;

/// Launch Huak's cli process.
pub fn main() -> CliResult {
pub fn main() {
let args = commands::args()
.version(clap::crate_version!())
.author(clap::crate_authors!())
.about("A Python package manager written in Rust inspired by Cargo")
.setting(AppSettings::ArgRequiredElseHelp);

run(args.get_matches())
let res = run(args.get_matches());
match res {
Ok(_) => (),
Err(err) => eprintln!("{}", err),
}
}

/// Command gating for Huak.
fn run(args: ArgMatches) -> CliResult {
fn run(args: ArgMatches) -> CliResult<()> {
match args.subcommand() {
Some(("activate", _)) => commands::activate::run(),
Some(("add", subargs)) => commands::add::run(subargs),
Expand All @@ -38,9 +42,6 @@ fn run(args: ArgMatches) -> CliResult {
Some(("update", subargs)) => commands::update::run(subargs),
Some(("test", _)) => commands::test::run(),
Some(("version", _)) => commands::version::run(),
_ => Err(CliError::new(
anyhow::format_err!("unrecognized command"),
2,
)),
_ => Err(CliError::new(CliErrorType::UnknownCommand)),
}
}
7 changes: 3 additions & 4 deletions src/huak/env/venv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,9 @@ impl Venv {
let from = match self.path.parent() {
Some(p) => p,
_ => {
return Err(CliError::new(
anyhow::format_err!("invalid venv path"),
2,
))
return Err(CliError::from(anyhow::format_err!(
"Invalid venv path"
)))
}
};

Expand Down
Loading

0 comments on commit 8a03059

Please sign in to comment.