From 011d9e3486174dbac2e506388239302b5bd8f4c7 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sun, 26 Feb 2023 17:11:15 -0800 Subject: [PATCH] workspace: make WorkspaceLoader::load() return WorkspaceLoadError I plan to make `RepoLoader::init()` return a `Result`, which means that `WorkspaceLoader::load()` will need to return more kinds of errors. Making it return `WorkspaceLoadError` is a good start. By also extracting a function for converting `WorkspaceLoadError` to `CommandError`, we can reuse a the handling of `PathError` in `cli_util`. --- lib/src/workspace.rs | 5 +++-- src/cli_util.rs | 10 +++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/src/workspace.rs b/lib/src/workspace.rs index eb2a11f1d..3f4e263d1 100644 --- a/lib/src/workspace.rs +++ b/lib/src/workspace.rs @@ -313,13 +313,14 @@ impl WorkspaceLoader { &self, user_settings: &UserSettings, store_factories: &StoreFactories, - ) -> Result { + ) -> Result { let repo_loader = RepoLoader::init(user_settings, &self.repo_dir, store_factories); let working_copy = WorkingCopy::load( repo_loader.store().clone(), self.workspace_root.clone(), self.working_copy_state_path.clone(), ); - Workspace::new(&self.workspace_root, working_copy, repo_loader) + let workspace = Workspace::new(&self.workspace_root, working_copy, repo_loader)?; + Ok(workspace) } } diff --git a/src/cli_util.rs b/src/cli_util.rs index f6183de24..c32d48b75 100644 --- a/src/cli_util.rs +++ b/src/cli_util.rs @@ -395,7 +395,7 @@ impl CommandHelper { let loader = self.workspace_loader()?; loader .load(&self.settings, &self.store_factories) - .map_err(|e| user_error(format!("{}: {}", e, e.error))) + .map_err(|err| map_workspace_load_error(err, &self.global_args)) } pub fn resolve_operation( @@ -1176,7 +1176,11 @@ fn init_workspace_loader( .unwrap_or(cwd) .to_owned() }; - WorkspaceLoader::init(&workspace_root).map_err(|err| match err { + WorkspaceLoader::init(&workspace_root).map_err(|err| map_workspace_load_error(err, global_args)) +} + +fn map_workspace_load_error(err: WorkspaceLoadError, global_args: &GlobalArgs) -> CommandError { + match err { WorkspaceLoadError::NoWorkspaceHere(wc_path) => { // Prefer user-specified workspace_path_str instead of absolute wc_path. let workspace_path_str = global_args.repository.as_deref().unwrap_or("."); @@ -1199,7 +1203,7 @@ jj init --git-repo=.", )), WorkspaceLoadError::Path(e) => user_error(format!("{}: {}", e, e.error)), WorkspaceLoadError::NonUnicodePath => user_error(err.to_string()), - }) + } } pub fn start_repo_transaction(