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`.
This commit is contained in:
Martin von Zweigbergk 2023-02-26 17:11:15 -08:00 committed by Martin von Zweigbergk
parent ea6a238c7c
commit 011d9e3486
2 changed files with 10 additions and 5 deletions

View file

@ -313,13 +313,14 @@ impl WorkspaceLoader {
&self,
user_settings: &UserSettings,
store_factories: &StoreFactories,
) -> Result<Workspace, PathError> {
) -> Result<Workspace, WorkspaceLoadError> {
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)
}
}

View file

@ -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(