Skip to content

Commit

Permalink
better error message
Browse files Browse the repository at this point in the history
  • Loading branch information
ityuany committed Jul 25, 2024
1 parent dee0af9 commit 2fcc07b
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 78 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"crossterm",
"dialoguer",
"flate",
"fmtln",
"hasher",
"indicatif",
"outpath",
Expand Down
16 changes: 4 additions & 12 deletions crates/snm_download_builder/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use colored::*;
use futures_util::StreamExt;
use indicatif::{ProgressBar, ProgressDrawTarget};
use reqwest::{Client, StatusCode};
use reqwest::Client;
use snm_utils::snm_error::SnmError;
use std::path::Path;
use std::time::Duration;
Expand Down Expand Up @@ -50,12 +50,6 @@ impl DownloadBuilder {
download_url: &str,
abs_path: P,
) -> Result<P, SnmError> {
// let response = Client::new().head(download_url).send().await?;

// if response.status() == StatusCode::NOT_FOUND {
// return Err(SnmError::NotFoundResourceError(download_url.to_string()));
// }

let mut attempts = 0;

while attempts < (self.retries + 1) {
Expand Down Expand Up @@ -91,7 +85,9 @@ impl DownloadBuilder {
if abs_path_ref.exists() {
match self.write_strategy {
WriteStrategy::Error => {
return Err(SnmError::FileAlreadyExists(abs_path_ref.to_path_buf()));
return Err(SnmError::FileAlreadyExists {
file_path: abs_path_ref.to_path_buf(),
});
}
WriteStrategy::WriteAfterDelete => {
std::fs::remove_file(&abs_path_ref)?;
Expand All @@ -113,10 +109,6 @@ impl DownloadBuilder {
.send()
.await?;

if response.status() == StatusCode::NOT_FOUND {
return Err(SnmError::NotFoundResourceError(download_url.to_string()));
}

if !response.status().is_success() {
return Err(SnmError::HttpStatusCodeUnOk);
}
Expand Down
8 changes: 3 additions & 5 deletions crates/snm_package_json/src/package_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{collections::HashMap, fs::File, io::BufReader, ops::Not, path::PathBuf

use regex::{Match, Regex};
use serde::Deserialize;
use snm_utils::snm_error::SnmError;
use snm_utils::{constant::PACKAGE_MANAGER, snm_error::SnmError};

use crate::{
package_manager_meta::{PackageManager, PackageManagerDownloadHash},
Expand Down Expand Up @@ -107,13 +107,11 @@ fn parse_package_manager(raw_package_manager: &str) -> Result<Option<PackageMana
raw_package_manager: raw_package_manager.to_string(),
})?;

let package_manager_vec = vec!["pnpm", "yarn", "npm"];

if package_manager_vec.contains(&name.as_str()).not() {
if PACKAGE_MANAGER.contains(&name.as_str()).not() {
return Err(SnmError::UnsupportedPackageManagerError {
name: name.to_string(),
raw: raw_package_manager.to_string(),
supported: package_manager_vec.iter().map(|s| s.to_string()).collect(),
supported: PACKAGE_MANAGER.iter().map(|s| s.to_string()).collect(),
});
}

Expand Down
3 changes: 0 additions & 3 deletions crates/snm_shim/src/get_node_bin_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use tracing::{instrument, Level};
#[instrument(level = Level::TRACE, ret)]
pub async fn get_node_bin_dir() -> Result<String, SnmError> {
let dir = current_dir()?;

let snm_config = parse_snm_config(&dir)?;

let snm_node = NodeAtom::new(snm_config.clone());
Expand Down Expand Up @@ -83,7 +82,5 @@ pub async fn get_node_bin_dir() -> Result<String, SnmError> {
}
let binary = snm_node.get_runtime_binary_dir_string(version.as_str())?;

// let binary_dir_string = ensure_binary_path(&snm_node, &version).await?;

Ok(binary)
}
6 changes: 2 additions & 4 deletions crates/snm_shim/src/package_manager_shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
use snm_atom::{atom::AtomTrait as _, package_manager_atom::PackageManagerAtom};
use snm_config::parse_snm_config;
use snm_download_builder::{DownloadBuilder, WriteStrategy};
use snm_utils::{exec::exec_cli, snm_error::SnmError};
use snm_utils::{constant::RESTRICTED_LIST, exec::exec_cli, snm_error::SnmError};

use crate::get_node_bin_dir::get_node_bin_dir;

Expand All @@ -31,8 +31,6 @@ pub async fn package_manager(prefix: &str, bin_name: &str) -> Result<(), SnmErro

let snm_package_manage = PackageManagerAtom::new(prefix, snm_config.clone());

let restricted_list = vec!["install", "i", "run"];

let bin_dirs = if let Some(package_manager) = snm_config.get_runtime_package_manager() {
tracing::trace!(
"There is a package manager in the entry process that is currently in use."
Expand Down Expand Up @@ -74,7 +72,7 @@ pub async fn package_manager(prefix: &str, bin_name: &str) -> Result<(), SnmErro
let binary = snm_package_manage.get_runtime_binary_dir_string(version.as_str())?;

vec![node_dir.clone(), binary]
} else if restricted_list.contains(&command.as_str()) {
} else if RESTRICTED_LIST.contains(&command.as_str()) {
return Err(SnmError::NotMatchPackageManagerError {
raw_command: args_all.join(" ").to_string(),
expect: package_manager.name,
Expand Down
3 changes: 3 additions & 0 deletions crates/snm_utils/src/constant.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub const PACKAGE_MANAGER: [&str; 3] = ["npm", "pnpm", "yarn"];

pub const RESTRICTED_LIST: [&str; 3] = ["install", "i", "run"];
4 changes: 3 additions & 1 deletion crates/snm_utils/src/exec.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{
env,
ffi::OsStr,
process::{Command, Stdio},
process::{exit, Command, Stdio},

Check warning on line 4 in crates/snm_utils/src/exec.rs

View workflow job for this annotation

GitHub Actions / build-and-test (ubuntu-latest, x86_64-unknown-linux-gnu)

unused import: `exit`

Check warning on line 4 in crates/snm_utils/src/exec.rs

View workflow job for this annotation

GitHub Actions / build-and-test (windows-latest, x86_64-pc-windows-msvc)

unused import: `exit`

Check warning on line 4 in crates/snm_utils/src/exec.rs

View workflow job for this annotation

GitHub Actions / build-and-test (windows-latest, i686-pc-windows-msvc)

unused import: `exit`

Check warning on line 4 in crates/snm_utils/src/exec.rs

View workflow job for this annotation

GitHub Actions / build-and-test (macos-14, aarch64-apple-darwin)

unused import: `exit`

Check warning on line 4 in crates/snm_utils/src/exec.rs

View workflow job for this annotation

GitHub Actions / build-and-test (macos-13, x86_64-apple-darwin)

unused import: `exit`
};

use crate::snm_error::SnmError;
Expand Down Expand Up @@ -32,6 +32,8 @@ where
return Err(SnmError::SNMBinaryProxyFail {
stderr: String::from_utf8_lossy(&output.stderr).to_string(),
});
// TODO
// exit(1);
}

print!("{}", String::from_utf8_lossy(&output.stdout).to_string());
Expand Down
7 changes: 7 additions & 0 deletions crates/snm_utils/src/fmtln.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[macro_export]
macro_rules! fmtln {
($fmt:expr, $($arg:tt)*) => {
format!($fmt, $($arg)*) + r#"
"#
};
}
2 changes: 2 additions & 0 deletions crates/snm_utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod constant;
pub mod exec;
pub mod fmtln;
pub mod snm_error;
pub mod to_ok;
88 changes: 35 additions & 53 deletions crates/snm_utils/src/snm_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use colored::*;
use std::{path::PathBuf, process::exit};
use thiserror::Error;

use crate::fmtln;

#[derive(Error, Debug)]
pub enum SnmError {
#[error("Build config error: {0}")]
Expand All @@ -25,9 +27,6 @@ pub enum SnmError {
#[error("Deserialize error: {0}")]
DeserializeError(#[from] serde_json::Error),

#[error("Not found: {0}")]
NotFoundResourceError(String),

#[error("Http status code not ok")]
HttpStatusCodeUnOk,

Expand All @@ -37,14 +36,11 @@ pub enum SnmError {
#[error("Get workspace dir error")]
GetWorkspaceError,

#[error("Not found valid version")]
NotFoundValidNodeVersionDeclaration,

#[error("No default node binary")]
NoDefaultNodeBinary,

#[error("File already exists {0}")]
FileAlreadyExists(PathBuf),
#[error("File already exists {file_path}")]
FileAlreadyExists { file_path: PathBuf },

#[error("Exceeded maximum retry attempts: {0}")]
ExceededMaxRetries(String),
Expand Down Expand Up @@ -97,7 +93,7 @@ pub fn create_error_message(message: String, descriptions: Vec<String>) -> Strin
.iter()
.map(|value| format!("{:<4}{}", "", value))
.collect::<Vec<String>>()
.join("\r\n".repeat(2).as_str());
.join("\r\n".repeat(1).as_str());

format!(
r##"
Expand All @@ -106,13 +102,26 @@ pub fn create_error_message(message: String, descriptions: Vec<String>) -> Strin
{:<3}{}
{}
"##,
"##,
"👹", message, "📋", "Explain", description
)
}

pub fn friendly_error_message(error: SnmError) {
match error {
SnmError::SNMBinaryProxyFail { stderr } => {
eprintln!(
r##"
👹 SNM proxy error info:
{}
"##,
stderr
)
}
SnmError::NoDefaultNodeBinary => {
eprintln!(r##"[error]: No default node binary"##);
}
SnmError::ParsePackageManagerError {
raw_package_manager,
} => {
Expand All @@ -135,23 +144,14 @@ pub fn friendly_error_message(error: SnmError) {
SnmError::ExceededMaxRetries(url) => {
let message = create_error_message(
"Exceeded max retries".to_string(),
vec![format!(
"The download failed after {} retries.",
url.to_string().bold().red()
)],
vec![
fmtln!("URL {}", url.to_string().bold().red()),
fmtln!("The download failed after 3 retries.",),
fmtln!("Please check the network connection and the download URL",),
],
);
eprintln!("{}", message);
}
SnmError::NotFoundResourceError(url) => {
eprintln!(
r##"
👹 Not found resource
The resource {} was not found.
"##,
url.to_string().bold().red()
);
}
SnmError::GetHomeDirError => {
eprintln!(
r##"
Expand Down Expand Up @@ -181,22 +181,17 @@ pub fn friendly_error_message(error: SnmError) {
"##
);
}
SnmError::FileAlreadyExists(path_buf) => {
eprintln!(
r##"
👹 File already exists
The file {} already exists.
"##,
path_buf.to_string_lossy().bold().red()
SnmError::FileAlreadyExists { file_path } => {
let message = create_error_message(
"File already exists".to_string(),
vec![format!(
"The file {} already exists.",
file_path.to_string_lossy().bold().red()
)],
);
eprintln!("{}", message);
}
SnmError::NotFoundValidNodeVersionDeclaration => {
eprintln!(r##"[error]: Not found valid node version declaration"##);
}
SnmError::NoDefaultNodeBinary => {
eprintln!(r##"[error]: No default node binary"##);
}

SnmError::NotFoundCommandError { bin_name } => {
let message = create_error_message(
format!("Not found command {}", bin_name.bold().red()),
Expand All @@ -222,16 +217,6 @@ pub fn friendly_error_message(error: SnmError) {
);
eprintln!("{}", message);
}
SnmError::SNMBinaryProxyFail { stderr } => {
eprintln!(
r##"
👹 SNM proxy error info:
{}
"##,
stderr
)
}
SnmError::ShasumError {
file_path,
expect,
Expand Down Expand Up @@ -272,7 +257,7 @@ pub fn friendly_error_message(error: SnmError) {
let message = create_error_message(
format!("Unsupported node {}", version.bold().bright_red()),
vec![
vec!["Only the following list is supported:".to_string()],
vec![fmtln!("{}", "Only the following list is supported:")],
node_white_list
.iter()
.map(|item| format!("- {}", item).to_string())
Expand All @@ -291,10 +276,7 @@ pub fn friendly_error_message(error: SnmError) {
format!("Unsupported packageManager {}", name.bold().bright_red()),
vec![
vec![
format!(
"The raw package manager configuration is {}, Only the following list is supported:",
raw.bold().bright_red()
),
fmtln!("The raw package manager configuration is {}, Only the following list is supported:", raw.bold().bright_red()),
],
supported
.iter()
Expand Down

0 comments on commit 2fcc07b

Please sign in to comment.