Skip to content

Commit

Permalink
workspace add: add filename context to FS error
Browse files Browse the repository at this point in the history
At work, a user encountered a panic upon attempting to create a dir at
the line in the diff below, but it turned out to be difficult to debug
because I didn't know what the path was. There already is a mechanism to
add path context in the lib crate; make it available in the cli crate as
well, and use the mechanism to add path context to "workspace add".
  • Loading branch information
jonathantanmy committed Jul 1, 2024
1 parent f883ce9 commit 1eef1a4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
6 changes: 6 additions & 0 deletions cli/src/command_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ impl From<io::Error> for CommandError {
}
}

impl From<jj_lib::file_util::PathError> for CommandError {
fn from(err: jj_lib::file_util::PathError) -> Self {
user_error(err)
}
}

impl From<config::ConfigError> for CommandError {
fn from(err: config::ConfigError) -> Self {
config_error(err)
Expand Down
3 changes: 2 additions & 1 deletion cli/src/commands/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use clap::Subcommand;
use itertools::Itertools;
use jj_lib::commit::CommitIteratorExt;
use jj_lib::file_util;
use jj_lib::file_util::IoResultExt;
use jj_lib::object_id::ObjectId;
use jj_lib::op_store::{OpStoreError, WorkspaceId};
use jj_lib::operation::Operation;
Expand Down Expand Up @@ -137,7 +138,7 @@ fn cmd_workspace_add(
if destination_path.exists() {
return Err(user_error("Workspace already exists"));
} else {
fs::create_dir(&destination_path).unwrap();
fs::create_dir(&destination_path).context(&destination_path)?;
}
let name = if let Some(name) = &args.name {
name.to_string()
Expand Down
2 changes: 1 addition & 1 deletion lib/src/file_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct PathError {
pub error: io::Error,
}

pub(crate) trait IoResultExt<T> {
pub trait IoResultExt<T> {
fn context(self, path: impl AsRef<Path>) -> Result<T, PathError>;
}

Expand Down

0 comments on commit 1eef1a4

Please sign in to comment.