Skip to content

Commit 2f92ac7

Browse files
committed
refactor(global-cli): use #[from] PmCli + is_user_message() helper
Replace the explicit per-variant `From<vite_pm_cli::Error>` impl (remapping UserMessage/Install/Workspace/etc. one-by-one) with an `#[error(transparent)] PmCli(#[from] vite_pm_cli::Error)` variant and an `Error::is_user_message()` helper that knows about both the top-level `UserMessage(_)` and the wrapped `PmCli(UserMessage(_))`. This drops the 12-line manual match in error.rs, localizes the "is friendly message" semantic on `Error` itself instead of leaking variant matches into main.rs, and makes adding future sub-error crates a one-line change to `is_user_message`. Also switch the two `eprintln!("{e}")` calls in main.rs that print user-facing messages to `output::raw_stderr` for consistency with the rest of the codebase's "go through `output::*` instead of raw print macros" rule.
1 parent ec1e361 commit 2f92ac7

2 files changed

Lines changed: 12 additions & 17 deletions

File tree

crates/vite_global_cli/src/error.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,15 @@ pub enum Error {
6464
version_source: String,
6565
help: String,
6666
},
67+
68+
#[error(transparent)]
69+
PmCli(#[from] vite_pm_cli::Error),
6770
}
6871

69-
// Explicit per-variant remap (not `#[from]`) so `UserMessage` stays a
70-
// distinct variant `main.rs` can pattern-match to skip the "error: "
71-
// prefix when the message originates from the PM crate.
72-
impl From<vite_pm_cli::Error> for Error {
73-
fn from(err: vite_pm_cli::Error) -> Self {
74-
match err {
75-
vite_pm_cli::Error::Install(e) => Self::Install(e),
76-
vite_pm_cli::Error::Workspace(e) => Self::Workspace(e),
77-
vite_pm_cli::Error::CommandExecution(e) => Self::CommandExecution(e),
78-
vite_pm_cli::Error::Json(e) => Self::JsonError(e),
79-
vite_pm_cli::Error::UserMessage(s) => Self::UserMessage(s),
80-
vite_pm_cli::Error::Other(s) => Self::Other(s),
81-
}
72+
impl Error {
73+
/// Whether this error should be printed without the "error: " prefix
74+
/// (a friendly user-facing message, not a stack trace).
75+
pub fn is_user_message(&self) -> bool {
76+
matches!(self, Self::UserMessage(_) | Self::PmCli(vite_pm_cli::Error::UserMessage(_)))
8277
}
8378
}

crates/vite_global_cli/src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ async fn run_corrected_args(cwd: &vite_path::AbsolutePathBuf, raw_args: &[String
196196
match run_command_with_options(cwd.clone(), parsed, render_options).await {
197197
Ok(exit_status) => exit_status_to_exit_code(exit_status),
198198
Err(e) => {
199-
if matches!(&e, error::Error::UserMessage(_)) {
200-
eprintln!("{e}");
199+
if e.is_user_message() {
200+
output::raw_stderr(&format!("{e}"));
201201
} else {
202202
output::error(&format!("{e}"));
203203
}
@@ -395,8 +395,8 @@ async fn main() -> ExitCode {
395395
Ok(args) => match run_command(cwd.clone(), args).await {
396396
Ok(exit_status) => exit_status_to_exit_code(exit_status),
397397
Err(e) => {
398-
if matches!(&e, error::Error::UserMessage(_)) {
399-
eprintln!("{e}");
398+
if e.is_user_message() {
399+
output::raw_stderr(&format!("{e}"));
400400
} else {
401401
output::error(&format!("{e}"));
402402
}

0 commit comments

Comments
 (0)