Skip to content

Commit

Permalink
refactor: simplify error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Jul 27, 2023
1 parent c774d60 commit 00cb921
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 23 deletions.
34 changes: 25 additions & 9 deletions crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,43 @@ async fn run_commands(cli: Cli) -> Result<()> {
match &cli.subcommand {
Subcommands::Init => {
// init command throws an error if package.json file exist.
PackageJson::init(&package_json_path)?;
PackageJson::init(&package_json_path).with_context(|| {
format!("Failed to initialize package.json at path {:?}", &package_json_path)
})?;
}
Subcommands::Add(args) => {
let mut package_manager = PackageManager::new(package_json_path)?;
let mut package_manager =
PackageManager::new(&package_json_path).with_context(|| {
format!("Failed to read package.json at path {:?}", &package_json_path)
})?;
// TODO if a package already exists in another dependency group, we don't remove
// the existing entry.
package_manager
.add(&args.package, args.get_dependency_group(), args.save_exact)
.await?;
.await
.with_context(|| format!("Failed to add package {}", &args.package))?;
}
Subcommands::Install(args) => {
let mut package_manager = PackageManager::new(package_json_path)?;
package_manager.install(args.dev, !args.no_optional).await?;
let mut package_manager =
PackageManager::new(&package_json_path).with_context(|| {
format!("Failed to read package.json at path {:?}", &package_json_path)
})?;
package_manager.install(args.dev, !args.no_optional).await.with_context(|| {
format!("Failed to install packages on {:?}", package_json_path)
})?;
}
Subcommands::Test => {
let package_json = PackageJson::from_path(&package_json_path)?;
let package_json = PackageJson::from_path(&package_json_path).with_context(|| {
format!("Failed to read package.json at path {:?}", &package_json_path)
})?;
if let Some(script) = package_json.get_script("test", false)? {
execute_shell(script)?;
}
}
Subcommands::Run(args) => {
let package_json = PackageJson::from_path(&package_json_path)?;
let package_json = PackageJson::from_path(&package_json_path).with_context(|| {
format!("Failed to read package.json at path {:?}", &package_json_path)
})?;
if let Some(script) = package_json.get_script(&args.command, args.if_present)? {
execute_shell(script)?;
}
Expand All @@ -57,7 +72,9 @@ async fn run_commands(cli: Cli) -> Result<()> {
// object. If no start property is specified on the scripts object, it will attempt to
// run node server.js as a default, failing if neither are present.
// The intended usage of the property is to specify a command that starts your program.
let package_json = PackageJson::from_path(&package_json_path)?;
let package_json = PackageJson::from_path(&package_json_path).with_context(|| {
format!("Failed to read package.json at path {:?}", &package_json_path)
})?;
if let Some(script) = package_json.get_script("start", true)? {
execute_shell(script)?;
} else {
Expand All @@ -84,7 +101,6 @@ mod tests {
env::set_current_dir(parent_folder.path()).unwrap();
let cli = Cli::parse_from(["", "init"]);
run_commands(cli).await.unwrap();

assert!(parent_folder.path().join("package.json").exists());
env::set_current_dir(&current_directory).unwrap();
}
Expand Down
2 changes: 1 addition & 1 deletion crates/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use thiserror::Error;
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum ExecutorError {
#[error("io error: {0}")]
#[error("io error")]
Io(#[from] std::io::Error),
}

Expand Down
4 changes: 2 additions & 2 deletions crates/lockfile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ use crate::package::LockfilePackage;
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum LockfileError {
#[error("filesystem error: `{0}`")]
#[error("filesystem error")]
FileSystem(#[from] std::io::Error),
#[error("serialization error: `{0}")]
#[error("serialization error")]
Serialization(#[from] serde_yaml::Error),
}

Expand Down
4 changes: 2 additions & 2 deletions crates/package_json/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use thiserror::Error;
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum PackageJsonError {
#[error("serialization failed: {0}")]
#[error("serialization failed with {0}")]
Serialization(#[from] serde_json::Error),
#[error("io error: `{0}`")]
#[error("io error")]
Io(#[from] std::io::Error),
#[error("package.json file already exists")]
AlreadyExist,
Expand Down
6 changes: 3 additions & 3 deletions crates/package_manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ pub mod install;
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum PackageManagerError {
#[error("tarball error: {0}")]
#[error("tarball error")]
Tarball(#[from] pacquet_tarball::TarballError),
#[error("package.json error: {0}")]
#[error("package.json error")]
PackageJson(#[from] pacquet_package_json::error::PackageJsonError),
#[error("registry error: {0}")]
#[error("registry error")]
Registry(#[from] pacquet_registry::RegistryError),
}

Expand Down
12 changes: 6 additions & 6 deletions crates/registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ use crate::package::{Package, PackageVersion};
pub enum RegistryError {
#[error("missing latest tag on {0}")]
MissingLatestTag(String),
#[error("missing version {0} on package {0}")]
#[error("missing version {0} on package {1}")]
MissingVersionRelease(String, String),
#[error("network error while fetching {0}")]
Network(#[from] reqwest::Error),
#[error("network middleware error")]
#[error("network middleware error with {0}")]
NetworkMiddleware(#[from] reqwest_middleware::Error),
#[error("io error {0}")]
#[error("io error with {0}")]
Io(#[from] std::io::Error),
#[error("serialization failed: {0}")]
#[error("serialization failed")]
Serialization(String),
#[error("tarball error: {0}")]
#[error("tarball error with {0}")]
Tarball(#[from] pacquet_tarball::TarballError),
#[error("package.json error: {0}")]
#[error("package.json error")]
PackageJson(#[from] pacquet_package_json::error::PackageJsonError),
}

Expand Down

0 comments on commit 00cb921

Please sign in to comment.