diff --git a/cli/src/command_error.rs b/cli/src/command_error.rs index 983a44c6f..2df88f91c 100644 --- a/cli/src/command_error.rs +++ b/cli/src/command_error.rs @@ -238,7 +238,10 @@ impl From for CommandError { impl From for CommandError { fn from(err: BackendError) -> Self { - internal_error_with_message("Unexpected error from backend", err) + match &err { + BackendError::Unsupported(_) => user_error(err), + _ => internal_error_with_message("Unexpected error from backend", err), + } } } @@ -305,7 +308,11 @@ want this file to be snapshotted. Otherwise add it to your `.gitignore` file."#, impl From for CommandError { fn from(err: TreeMergeError) -> Self { - internal_error_with_message("Merge failed", err) + let kind = match &err { + TreeMergeError::BackendError(BackendError::Unsupported(_)) => CommandErrorKind::User, + _ => CommandErrorKind::Internal, + }; + CommandError::with_message(kind, "Merge failed", err) } } diff --git a/cli/tests/test_abandon_command.rs b/cli/tests/test_abandon_command.rs index ea510aa8b..2f8b9d3d6 100644 --- a/cli/tests/test_abandon_command.rs +++ b/cli/tests/test_abandon_command.rs @@ -314,9 +314,9 @@ fn test_bug_2600_rootcommit_special_case() { "###); // Now, the test - let stderr = test_env.jj_cmd_internal_error(&repo_path, &["abandon", "base"]); + let stderr = test_env.jj_cmd_failure(&repo_path, &["abandon", "base"]); insta::assert_snapshot!(stderr, @r###" - Internal error: Merge failed + Error: Merge failed Caused by: The Git backend does not support creating merge commits with the root commit as one of the parents. "###); } diff --git a/lib/src/backend.rs b/lib/src/backend.rs index 6a4b54cc8..1b88e9a60 100644 --- a/lib/src/backend.rs +++ b/lib/src/backend.rs @@ -200,6 +200,10 @@ pub enum BackendError { }, #[error(transparent)] Other(Box), + /// A valid operation attempted, but failed because it isn't supported by + /// the particular backend. + #[error("{0}")] + Unsupported(String), } pub type BackendResult = Result; diff --git a/lib/src/git_backend.rs b/lib/src/git_backend.rs index d9dc9a89d..fb21dd649 100644 --- a/lib/src/git_backend.rs +++ b/lib/src/git_backend.rs @@ -1121,10 +1121,10 @@ impl Backend for GitBackend { // there are no other parents since Git cannot represent a merge between a root // commit and another commit. if contents.parents.len() > 1 { - return Err(BackendError::Other( + return Err(BackendError::Unsupported( "The Git backend does not support creating merge commits with the root \ commit as one of the parents." - .into(), + .to_owned(), )); } } else { @@ -1702,7 +1702,7 @@ mod tests { commit.parents = vec![first_id, backend.root_commit_id().clone()]; assert_matches!( backend.write_commit(commit, None), - Err(BackendError::Other(err)) if err.to_string().contains("root commit") + Err(BackendError::Unsupported(message)) if message.contains("root commit") ); }