Skip to main content

jj_cli/
command_error.rs

1// Copyright 2022-2024 The Jujutsu Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::error;
16use std::error::Error as _;
17use std::io;
18use std::io::Write as _;
19use std::iter;
20use std::sync::Arc;
21
22use itertools::Itertools as _;
23use jj_lib::absorb::AbsorbError;
24use jj_lib::backend::BackendError;
25use jj_lib::backend::CommitId;
26use jj_lib::bisect::BisectionError;
27use jj_lib::config::ConfigFileSaveError;
28use jj_lib::config::ConfigGetError;
29use jj_lib::config::ConfigLoadError;
30use jj_lib::config::ConfigMigrateError;
31use jj_lib::dsl_util::Diagnostics;
32use jj_lib::evolution::WalkPredecessorsError;
33use jj_lib::fileset::FilePatternParseError;
34use jj_lib::fileset::FilesetParseError;
35use jj_lib::fileset::FilesetParseErrorKind;
36use jj_lib::fix::FixError;
37use jj_lib::gitignore::GitIgnoreError;
38use jj_lib::index::IndexError;
39use jj_lib::op_heads_store::OpHeadResolutionError;
40use jj_lib::op_heads_store::OpHeadsStoreError;
41use jj_lib::op_store::OpStoreError;
42use jj_lib::op_walk::OpsetEvaluationError;
43use jj_lib::op_walk::OpsetResolutionError;
44use jj_lib::repo::CheckOutCommitError;
45use jj_lib::repo::EditCommitError;
46use jj_lib::repo::RepoLoaderError;
47use jj_lib::repo::RewriteRootCommit;
48use jj_lib::repo_path::RepoPathBuf;
49use jj_lib::repo_path::UiPathParseError;
50use jj_lib::revset;
51use jj_lib::revset::RevsetEvaluationError;
52use jj_lib::revset::RevsetParseError;
53use jj_lib::revset::RevsetParseErrorKind;
54use jj_lib::revset::RevsetResolutionError;
55use jj_lib::secure_config::SecureConfigError;
56use jj_lib::str_util::StringPatternParseError;
57use jj_lib::trailer::TrailerParseError;
58use jj_lib::transaction::TransactionCommitError;
59use jj_lib::view::RenameWorkspaceError;
60use jj_lib::working_copy::RecoverWorkspaceError;
61use jj_lib::working_copy::ResetError;
62use jj_lib::working_copy::SnapshotError;
63use jj_lib::working_copy::WorkingCopyStateError;
64use jj_lib::workspace::WorkspaceInitError;
65use jj_lib::workspace_store::WorkspaceStoreError;
66use thiserror::Error;
67
68use crate::cli_util::short_operation_hash;
69use crate::description_util::ParseBulkEditMessageError;
70use crate::description_util::TempTextEditError;
71use crate::description_util::TextEditError;
72use crate::diff_util::DiffRenderError;
73use crate::formatter::FormatRecorder;
74use crate::formatter::Formatter;
75use crate::formatter::FormatterExt as _;
76use crate::merge_tools::ConflictResolveError;
77use crate::merge_tools::DiffEditError;
78use crate::merge_tools::MergeToolConfigError;
79use crate::merge_tools::MergeToolPartialResolutionError;
80use crate::revset_util::BookmarkNameParseError;
81use crate::revset_util::TagNameParseError;
82use crate::revset_util::UserRevsetEvaluationError;
83use crate::template_parser::TemplateParseError;
84use crate::template_parser::TemplateParseErrorKind;
85use crate::ui::Ui;
86
87#[derive(Clone, Copy, Debug, Eq, PartialEq)]
88pub enum CommandErrorKind {
89    User,
90    Config,
91    /// Invalid command line. The inner error type may be `clap::Error`.
92    Cli,
93    BrokenPipe,
94    Internal,
95}
96
97#[derive(Clone, Debug)]
98pub struct CommandError {
99    pub kind: CommandErrorKind,
100    pub error: Arc<dyn error::Error + Send + Sync>,
101    pub hints: Vec<ErrorHint>,
102}
103
104impl CommandError {
105    pub fn new(
106        kind: CommandErrorKind,
107        err: impl Into<Box<dyn error::Error + Send + Sync>>,
108    ) -> Self {
109        Self {
110            kind,
111            error: Arc::from(err.into()),
112            hints: vec![],
113        }
114    }
115
116    pub fn with_message(
117        kind: CommandErrorKind,
118        message: impl Into<String>,
119        source: impl Into<Box<dyn error::Error + Send + Sync>>,
120    ) -> Self {
121        Self::new(kind, ErrorWithMessage::new(message, source))
122    }
123
124    /// Returns error with the given plain-text `hint` attached.
125    pub fn hinted(mut self, hint: impl Into<String>) -> Self {
126        self.add_hint(hint);
127        self
128    }
129
130    /// Appends plain-text `hint` to the error.
131    pub fn add_hint(&mut self, hint: impl Into<String>) {
132        self.hints.push(ErrorHint::PlainText(hint.into()));
133    }
134
135    /// Appends formatted `hint` to the error.
136    pub fn add_formatted_hint(&mut self, hint: FormatRecorder) {
137        self.hints.push(ErrorHint::Formatted(hint));
138    }
139
140    /// Constructs formatted hint and appends it to the error.
141    pub fn add_formatted_hint_with(
142        &mut self,
143        write: impl FnOnce(&mut dyn Formatter) -> io::Result<()>,
144    ) {
145        let mut formatter = FormatRecorder::new(true);
146        write(&mut formatter).expect("write() to FormatRecorder should never fail");
147        self.add_formatted_hint(formatter);
148    }
149
150    /// Appends 0 or more plain-text `hints` to the error.
151    pub fn extend_hints(&mut self, hints: impl IntoIterator<Item = String>) {
152        self.hints
153            .extend(hints.into_iter().map(ErrorHint::PlainText));
154    }
155}
156
157#[derive(Clone, Debug)]
158pub enum ErrorHint {
159    PlainText(String),
160    Formatted(FormatRecorder),
161}
162
163/// Wraps error with user-visible message.
164#[derive(Debug, Error)]
165#[error("{message}")]
166struct ErrorWithMessage {
167    message: String,
168    source: Box<dyn error::Error + Send + Sync>,
169}
170
171impl ErrorWithMessage {
172    fn new(
173        message: impl Into<String>,
174        source: impl Into<Box<dyn error::Error + Send + Sync>>,
175    ) -> Self {
176        Self {
177            message: message.into(),
178            source: source.into(),
179        }
180    }
181}
182
183pub fn user_error(err: impl Into<Box<dyn error::Error + Send + Sync>>) -> CommandError {
184    CommandError::new(CommandErrorKind::User, err)
185}
186
187pub fn user_error_with_message(
188    message: impl Into<String>,
189    source: impl Into<Box<dyn error::Error + Send + Sync>>,
190) -> CommandError {
191    CommandError::with_message(CommandErrorKind::User, message, source)
192}
193
194pub fn config_error(err: impl Into<Box<dyn error::Error + Send + Sync>>) -> CommandError {
195    CommandError::new(CommandErrorKind::Config, err)
196}
197
198pub fn config_error_with_message(
199    message: impl Into<String>,
200    source: impl Into<Box<dyn error::Error + Send + Sync>>,
201) -> CommandError {
202    CommandError::with_message(CommandErrorKind::Config, message, source)
203}
204
205pub fn cli_error(err: impl Into<Box<dyn error::Error + Send + Sync>>) -> CommandError {
206    CommandError::new(CommandErrorKind::Cli, err)
207}
208
209pub fn cli_error_with_message(
210    message: impl Into<String>,
211    source: impl Into<Box<dyn error::Error + Send + Sync>>,
212) -> CommandError {
213    CommandError::with_message(CommandErrorKind::Cli, message, source)
214}
215
216pub fn internal_error(err: impl Into<Box<dyn error::Error + Send + Sync>>) -> CommandError {
217    CommandError::new(CommandErrorKind::Internal, err)
218}
219
220pub fn internal_error_with_message(
221    message: impl Into<String>,
222    source: impl Into<Box<dyn error::Error + Send + Sync>>,
223) -> CommandError {
224    CommandError::with_message(CommandErrorKind::Internal, message, source)
225}
226
227fn format_similarity_hint<S: AsRef<str>>(candidates: &[S]) -> Option<String> {
228    match candidates {
229        [] => None,
230        names => {
231            let quoted_names = names.iter().map(|s| format!("`{}`", s.as_ref())).join(", ");
232            Some(format!("Did you mean {quoted_names}?"))
233        }
234    }
235}
236
237impl From<io::Error> for CommandError {
238    fn from(err: io::Error) -> Self {
239        let kind = match err.kind() {
240            io::ErrorKind::BrokenPipe => CommandErrorKind::BrokenPipe,
241            _ => CommandErrorKind::User,
242        };
243        Self::new(kind, err)
244    }
245}
246
247impl From<jj_lib::file_util::PathError> for CommandError {
248    fn from(err: jj_lib::file_util::PathError) -> Self {
249        user_error(err)
250    }
251}
252
253impl From<ConfigFileSaveError> for CommandError {
254    fn from(err: ConfigFileSaveError) -> Self {
255        user_error(err)
256    }
257}
258
259impl From<ConfigGetError> for CommandError {
260    fn from(err: ConfigGetError) -> Self {
261        let hint = config_get_error_hint(&err);
262        let mut cmd_err = config_error(err);
263        cmd_err.extend_hints(hint);
264        cmd_err
265    }
266}
267
268impl From<ConfigLoadError> for CommandError {
269    fn from(err: ConfigLoadError) -> Self {
270        let hint = match &err {
271            ConfigLoadError::Read(_) => None,
272            ConfigLoadError::Parse { source_path, .. } => source_path
273                .as_ref()
274                .map(|path| format!("Check the config file: {}", path.display())),
275        };
276        let mut cmd_err = config_error(err);
277        cmd_err.extend_hints(hint);
278        cmd_err
279    }
280}
281
282impl From<ConfigMigrateError> for CommandError {
283    fn from(err: ConfigMigrateError) -> Self {
284        let hint = err
285            .source_path
286            .as_ref()
287            .map(|path| format!("Check the config file: {}", path.display()));
288        let mut cmd_err = config_error(err);
289        cmd_err.extend_hints(hint);
290        cmd_err
291    }
292}
293
294impl From<RewriteRootCommit> for CommandError {
295    fn from(err: RewriteRootCommit) -> Self {
296        internal_error_with_message("Attempted to rewrite the root commit", err)
297    }
298}
299
300impl From<EditCommitError> for CommandError {
301    fn from(err: EditCommitError) -> Self {
302        internal_error_with_message("Failed to edit a commit", err)
303    }
304}
305
306impl From<CheckOutCommitError> for CommandError {
307    fn from(err: CheckOutCommitError) -> Self {
308        internal_error_with_message("Failed to check out a commit", err)
309    }
310}
311
312impl From<RenameWorkspaceError> for CommandError {
313    fn from(err: RenameWorkspaceError) -> Self {
314        user_error_with_message("Failed to rename a workspace", err)
315    }
316}
317
318impl From<BackendError> for CommandError {
319    fn from(err: BackendError) -> Self {
320        match &err {
321            BackendError::Unsupported(_) => user_error(err),
322            _ => internal_error_with_message("Unexpected error from backend", err),
323        }
324    }
325}
326
327impl From<IndexError> for CommandError {
328    fn from(err: IndexError) -> Self {
329        internal_error_with_message("Unexpected error from index", err)
330    }
331}
332
333impl From<OpHeadsStoreError> for CommandError {
334    fn from(err: OpHeadsStoreError) -> Self {
335        internal_error_with_message("Unexpected error from operation heads store", err)
336    }
337}
338
339impl From<WorkspaceStoreError> for CommandError {
340    fn from(err: WorkspaceStoreError) -> Self {
341        internal_error_with_message("Unexpected error from workspace store", err)
342    }
343}
344
345impl From<WorkspaceInitError> for CommandError {
346    fn from(err: WorkspaceInitError) -> Self {
347        match err {
348            WorkspaceInitError::DestinationExists(_) => {
349                user_error("The target repo already exists")
350            }
351            WorkspaceInitError::EncodeRepoPath(_) => user_error(err),
352            WorkspaceInitError::CheckOutCommit(err) => {
353                internal_error_with_message("Failed to check out the initial commit", err)
354            }
355            WorkspaceInitError::Path(err) => {
356                internal_error_with_message("Failed to access the repository", err)
357            }
358            WorkspaceInitError::OpHeadsStore(err) => {
359                user_error_with_message("Failed to record initial operation", err)
360            }
361            WorkspaceInitError::WorkspaceStore(err) => {
362                internal_error_with_message("Failed to record workspace path", err)
363            }
364            WorkspaceInitError::Backend(err) => {
365                user_error_with_message("Failed to access the repository", err)
366            }
367            WorkspaceInitError::WorkingCopyState(err) => {
368                internal_error_with_message("Failed to access the repository", err)
369            }
370            WorkspaceInitError::SignInit(err) => user_error(err),
371            WorkspaceInitError::TransactionCommit(err) => err.into(),
372        }
373    }
374}
375
376impl From<OpHeadResolutionError> for CommandError {
377    fn from(err: OpHeadResolutionError) -> Self {
378        match err {
379            OpHeadResolutionError::NoHeads => {
380                internal_error_with_message("Corrupt repository", err)
381            }
382        }
383    }
384}
385
386impl From<OpsetEvaluationError> for CommandError {
387    fn from(err: OpsetEvaluationError) -> Self {
388        match err {
389            OpsetEvaluationError::OpsetResolution(err) => {
390                let hint = opset_resolution_error_hint(&err);
391                let mut cmd_err = user_error(err);
392                cmd_err.extend_hints(hint);
393                cmd_err
394            }
395            OpsetEvaluationError::OpHeadResolution(err) => err.into(),
396            OpsetEvaluationError::OpHeadsStore(err) => err.into(),
397            OpsetEvaluationError::OpStore(err) => err.into(),
398        }
399    }
400}
401
402impl From<SnapshotError> for CommandError {
403    fn from(err: SnapshotError) -> Self {
404        internal_error_with_message("Failed to snapshot the working copy", err)
405    }
406}
407
408impl From<OpStoreError> for CommandError {
409    fn from(err: OpStoreError) -> Self {
410        internal_error_with_message("Failed to load an operation", err)
411    }
412}
413
414impl From<RepoLoaderError> for CommandError {
415    fn from(err: RepoLoaderError) -> Self {
416        internal_error_with_message("Failed to load the repo", err)
417    }
418}
419
420impl From<ResetError> for CommandError {
421    fn from(err: ResetError) -> Self {
422        internal_error_with_message("Failed to reset the working copy", err)
423    }
424}
425
426impl From<TransactionCommitError> for CommandError {
427    fn from(err: TransactionCommitError) -> Self {
428        internal_error(err)
429    }
430}
431
432impl From<WalkPredecessorsError> for CommandError {
433    fn from(err: WalkPredecessorsError) -> Self {
434        match err {
435            WalkPredecessorsError::Backend(err) => err.into(),
436            WalkPredecessorsError::Index(err) => err.into(),
437            WalkPredecessorsError::OpStore(err) => err.into(),
438            WalkPredecessorsError::CycleDetected(_) => internal_error(err),
439        }
440    }
441}
442
443impl From<DiffEditError> for CommandError {
444    fn from(err: DiffEditError) -> Self {
445        user_error_with_message("Failed to edit diff", err)
446    }
447}
448
449impl From<DiffRenderError> for CommandError {
450    fn from(err: DiffRenderError) -> Self {
451        match err {
452            DiffRenderError::DiffGenerate(_) => user_error(err),
453            DiffRenderError::Backend(err) => err.into(),
454            DiffRenderError::AccessDenied { .. } => user_error(err),
455            DiffRenderError::InvalidRepoPath(_) => user_error(err),
456            DiffRenderError::Io(err) => err.into(),
457        }
458    }
459}
460
461impl From<ConflictResolveError> for CommandError {
462    fn from(err: ConflictResolveError) -> Self {
463        match err {
464            ConflictResolveError::Backend(err) => err.into(),
465            ConflictResolveError::Io(err) => err.into(),
466            _ => {
467                let hint = match &err {
468                    ConflictResolveError::ConflictTooComplicated { .. } => {
469                        Some("Edit the conflict markers manually to resolve this.".to_owned())
470                    }
471                    ConflictResolveError::ExecutableConflict { .. } => {
472                        Some("Use `jj file chmod` to update the executable bit.".to_owned())
473                    }
474                    _ => None,
475                };
476                let mut cmd_err = user_error_with_message("Failed to resolve conflicts", err);
477                cmd_err.extend_hints(hint);
478                cmd_err
479            }
480        }
481    }
482}
483
484impl From<MergeToolPartialResolutionError> for CommandError {
485    fn from(err: MergeToolPartialResolutionError) -> Self {
486        user_error(err)
487    }
488}
489
490impl From<MergeToolConfigError> for CommandError {
491    fn from(err: MergeToolConfigError) -> Self {
492        match &err {
493            MergeToolConfigError::MergeArgsNotConfigured { tool_name } => {
494                let tool_name = tool_name.clone();
495                user_error(err).hinted(format!(
496                    "To use `{tool_name}` as a merge tool, the config \
497                     `merge-tools.{tool_name}.merge-args` must be defined (see docs for details)"
498                ))
499            }
500            _ => user_error_with_message("Failed to load tool configuration", err),
501        }
502    }
503}
504
505impl From<TextEditError> for CommandError {
506    fn from(err: TextEditError) -> Self {
507        user_error(err)
508    }
509}
510
511impl From<TempTextEditError> for CommandError {
512    fn from(err: TempTextEditError) -> Self {
513        let hint = err.path.as_ref().map(|path| {
514            let name = err.name.as_deref().unwrap_or("file");
515            format!("Edited {name} is left in {path}", path = path.display())
516        });
517        let mut cmd_err = user_error(err);
518        cmd_err.extend_hints(hint);
519        cmd_err
520    }
521}
522
523impl From<TrailerParseError> for CommandError {
524    fn from(err: TrailerParseError) -> Self {
525        user_error(err)
526    }
527}
528
529#[cfg(feature = "git")]
530mod git {
531    use jj_lib::git::GitDefaultRefspecError;
532    use jj_lib::git::GitExportError;
533    use jj_lib::git::GitFetchError;
534    use jj_lib::git::GitImportError;
535    use jj_lib::git::GitPushError;
536    use jj_lib::git::GitRefExpansionError;
537    use jj_lib::git::GitRemoteManagementError;
538    use jj_lib::git::GitResetHeadError;
539    use jj_lib::git::UnexpectedGitBackendError;
540
541    use super::*;
542
543    impl From<GitImportError> for CommandError {
544        fn from(err: GitImportError) -> Self {
545            let hint = match &err {
546                GitImportError::MissingHeadTarget { .. }
547                | GitImportError::MissingRefAncestor { .. } => Some(
548                    "\
549Is this Git repository a partial clone (cloned with the --filter argument)?
550jj currently does not support partial clones. To use jj with this repository, try re-cloning with \
551                     the full repository contents."
552                        .to_string(),
553                ),
554                GitImportError::Backend(_) => None,
555                GitImportError::Index(_) => None,
556                GitImportError::Git(_) => None,
557                GitImportError::UnexpectedBackend(_) => None,
558            };
559            let mut cmd_err =
560                user_error_with_message("Failed to import refs from underlying Git repo", err);
561            cmd_err.extend_hints(hint);
562            cmd_err
563        }
564    }
565
566    impl From<GitExportError> for CommandError {
567        fn from(err: GitExportError) -> Self {
568            user_error_with_message("Failed to export refs to underlying Git repo", err)
569        }
570    }
571
572    impl From<GitFetchError> for CommandError {
573        fn from(err: GitFetchError) -> Self {
574            match err {
575                GitFetchError::NoSuchRemote(_) => user_error(err),
576                GitFetchError::RemoteName(_) => {
577                    user_error(err).hinted("Run `jj git remote rename` to give a different name.")
578                }
579                GitFetchError::RejectedUpdates(_) | GitFetchError::Subprocess(_) => user_error(err),
580            }
581        }
582    }
583
584    impl From<GitDefaultRefspecError> for CommandError {
585        fn from(err: GitDefaultRefspecError) -> Self {
586            match err {
587                GitDefaultRefspecError::NoSuchRemote(_) => user_error(err),
588                GitDefaultRefspecError::InvalidRemoteConfiguration(_, _) => user_error(err),
589            }
590        }
591    }
592
593    impl From<GitRefExpansionError> for CommandError {
594        fn from(err: GitRefExpansionError) -> Self {
595            match &err {
596                GitRefExpansionError::Expression(_) => user_error(err)
597                    .hinted("Specify patterns in `(positive | ...) & ~(negative | ...)` form."),
598                GitRefExpansionError::InvalidBranchPattern(_) => user_error(err),
599            }
600        }
601    }
602
603    impl From<GitPushError> for CommandError {
604        fn from(err: GitPushError) -> Self {
605            match err {
606                GitPushError::NoSuchRemote(_) => user_error(err),
607                GitPushError::RemoteName(_) => {
608                    user_error(err).hinted("Run `jj git remote rename` to give a different name.")
609                }
610                GitPushError::Subprocess(_) => user_error(err),
611                GitPushError::UnexpectedBackend(_) => user_error(err),
612            }
613        }
614    }
615
616    impl From<GitRemoteManagementError> for CommandError {
617        fn from(err: GitRemoteManagementError) -> Self {
618            user_error(err)
619        }
620    }
621
622    impl From<GitResetHeadError> for CommandError {
623        fn from(err: GitResetHeadError) -> Self {
624            user_error_with_message("Failed to reset Git HEAD state", err)
625        }
626    }
627
628    impl From<UnexpectedGitBackendError> for CommandError {
629        fn from(err: UnexpectedGitBackendError) -> Self {
630            user_error(err)
631        }
632    }
633}
634
635impl From<RevsetEvaluationError> for CommandError {
636    fn from(err: RevsetEvaluationError) -> Self {
637        user_error(err)
638    }
639}
640
641impl From<FilesetParseError> for CommandError {
642    fn from(err: FilesetParseError) -> Self {
643        let hint = fileset_parse_error_hint(&err);
644        let mut cmd_err =
645            user_error_with_message(format!("Failed to parse fileset: {}", err.kind()), err);
646        cmd_err.extend_hints(hint);
647        cmd_err
648    }
649}
650
651impl From<RecoverWorkspaceError> for CommandError {
652    fn from(err: RecoverWorkspaceError) -> Self {
653        match err {
654            RecoverWorkspaceError::Backend(err) => err.into(),
655            RecoverWorkspaceError::Reset(err) => err.into(),
656            RecoverWorkspaceError::RewriteRootCommit(err) => err.into(),
657            RecoverWorkspaceError::TransactionCommit(err) => err.into(),
658            err @ RecoverWorkspaceError::WorkspaceMissingWorkingCopy(_) => user_error(err),
659        }
660    }
661}
662
663impl From<RevsetParseError> for CommandError {
664    fn from(err: RevsetParseError) -> Self {
665        let hint = revset_parse_error_hint(&err);
666        let mut cmd_err =
667            user_error_with_message(format!("Failed to parse revset: {}", err.kind()), err);
668        cmd_err.extend_hints(hint);
669        cmd_err
670    }
671}
672
673impl From<RevsetResolutionError> for CommandError {
674    fn from(err: RevsetResolutionError) -> Self {
675        let hints = revset_resolution_error_hints(&err);
676        let mut cmd_err = user_error(err);
677        cmd_err.extend_hints(hints);
678        cmd_err
679    }
680}
681
682impl From<UserRevsetEvaluationError> for CommandError {
683    fn from(err: UserRevsetEvaluationError) -> Self {
684        match err {
685            UserRevsetEvaluationError::Resolution(err) => err.into(),
686            UserRevsetEvaluationError::Evaluation(err) => err.into(),
687        }
688    }
689}
690
691impl From<TemplateParseError> for CommandError {
692    fn from(err: TemplateParseError) -> Self {
693        let hint = template_parse_error_hint(&err);
694        let mut cmd_err =
695            user_error_with_message(format!("Failed to parse template: {}", err.kind()), err);
696        cmd_err.extend_hints(hint);
697        cmd_err
698    }
699}
700
701impl From<UiPathParseError> for CommandError {
702    fn from(err: UiPathParseError) -> Self {
703        user_error(err)
704    }
705}
706
707impl From<clap::Error> for CommandError {
708    fn from(err: clap::Error) -> Self {
709        let hint = find_source_parse_error_hint(&err);
710        let mut cmd_err = cli_error(err);
711        cmd_err.extend_hints(hint);
712        cmd_err
713    }
714}
715
716impl From<WorkingCopyStateError> for CommandError {
717    fn from(err: WorkingCopyStateError) -> Self {
718        internal_error_with_message("Failed to access working copy state", err)
719    }
720}
721
722impl From<GitIgnoreError> for CommandError {
723    fn from(err: GitIgnoreError) -> Self {
724        user_error_with_message("Failed to process .gitignore.", err)
725    }
726}
727
728impl From<ParseBulkEditMessageError> for CommandError {
729    fn from(err: ParseBulkEditMessageError) -> Self {
730        user_error(err)
731    }
732}
733
734impl From<AbsorbError> for CommandError {
735    fn from(err: AbsorbError) -> Self {
736        match err {
737            AbsorbError::Backend(err) => err.into(),
738            AbsorbError::RevsetEvaluation(err) => err.into(),
739        }
740    }
741}
742
743impl From<FixError> for CommandError {
744    fn from(err: FixError) -> Self {
745        match err {
746            FixError::Backend(err) => err.into(),
747            FixError::RevsetEvaluation(err) => err.into(),
748            FixError::Io(err) => err.into(),
749            FixError::FixContent(err) => internal_error_with_message(
750                "An error occurred while attempting to fix file content",
751                err,
752            ),
753        }
754    }
755}
756
757impl From<BisectionError> for CommandError {
758    fn from(err: BisectionError) -> Self {
759        match err {
760            BisectionError::BackendError(_) => user_error(err),
761            BisectionError::RevsetEvaluationError(_) => user_error(err),
762        }
763    }
764}
765
766impl From<SecureConfigError> for CommandError {
767    fn from(err: SecureConfigError) -> Self {
768        internal_error_with_message("Failed to determine the secure config for a repo", err)
769    }
770}
771
772fn find_source_parse_error_hint(err: &dyn error::Error) -> Option<String> {
773    let source = err.source()?;
774    if let Some(source) = source.downcast_ref() {
775        bookmark_name_parse_error_hint(source)
776    } else if let Some(source) = source.downcast_ref() {
777        config_get_error_hint(source)
778    } else if let Some(source) = source.downcast_ref() {
779        file_pattern_parse_error_hint(source)
780    } else if let Some(source) = source.downcast_ref() {
781        fileset_parse_error_hint(source)
782    } else if let Some(source) = source.downcast_ref() {
783        revset_parse_error_hint(source)
784    } else if let Some(source) = source.downcast_ref() {
785        // TODO: propagate all hints?
786        revset_resolution_error_hints(source).into_iter().next()
787    } else if let Some(UserRevsetEvaluationError::Resolution(source)) = source.downcast_ref() {
788        // TODO: propagate all hints?
789        revset_resolution_error_hints(source).into_iter().next()
790    } else if let Some(source) = source.downcast_ref() {
791        string_pattern_parse_error_hint(source)
792    } else if let Some(source) = source.downcast_ref() {
793        tag_name_parse_error_hint(source)
794    } else if let Some(source) = source.downcast_ref() {
795        template_parse_error_hint(source)
796    } else {
797        None
798    }
799}
800
801const REVSET_SYMBOL_HINT: &str = "See https://docs.jj-vcs.dev/latest/revsets/ or use `jj help -k \
802                                  revsets` for how to quote symbols.";
803
804fn bookmark_name_parse_error_hint(err: &BookmarkNameParseError) -> Option<String> {
805    use revset::ExpressionKind;
806    match revset::parse_program(&err.input).map(|node| node.kind) {
807        Ok(ExpressionKind::RemoteSymbol(symbol)) => Some(format!(
808            "Looks like remote bookmark. Run `jj bookmark track {name} --remote={remote}` to \
809             track it.",
810            name = symbol.name.as_symbol(),
811            remote = symbol.remote.as_symbol()
812        )),
813        _ => Some(REVSET_SYMBOL_HINT.to_owned()),
814    }
815}
816
817fn config_get_error_hint(err: &ConfigGetError) -> Option<String> {
818    match &err {
819        ConfigGetError::NotFound { .. } => None,
820        ConfigGetError::Type { source_path, .. } => source_path
821            .as_ref()
822            .map(|path| format!("Check the config file: {}", path.display())),
823    }
824}
825
826fn file_pattern_parse_error_hint(err: &FilePatternParseError) -> Option<String> {
827    match err {
828        FilePatternParseError::InvalidKind(_) => Some(String::from(
829            "See https://docs.jj-vcs.dev/latest/filesets/#file-patterns or `jj help -k filesets` \
830             for valid prefixes.",
831        )),
832        // Suggest root:"<path>" if input can be parsed as repo-relative path
833        FilePatternParseError::UiPath(UiPathParseError::Fs(e)) => {
834            RepoPathBuf::from_relative_path(&e.input).ok().map(|path| {
835                format!(r#"Consider using root:{path:?} to specify repo-relative path"#)
836            })
837        }
838        FilePatternParseError::RelativePath(_) => None,
839        FilePatternParseError::GlobPattern(_) => None,
840    }
841}
842
843fn fileset_parse_error_hint(err: &FilesetParseError) -> Option<String> {
844    match err.kind() {
845        FilesetParseErrorKind::SyntaxError => Some(String::from(
846            "See https://docs.jj-vcs.dev/latest/filesets/ or use `jj help -k filesets` for \
847             filesets syntax and how to match file paths.",
848        )),
849        FilesetParseErrorKind::NoSuchFunction {
850            name: _,
851            candidates,
852        } => format_similarity_hint(candidates),
853        FilesetParseErrorKind::InvalidArguments { .. } => find_source_parse_error_hint(&err),
854        FilesetParseErrorKind::RedefinedFunctionParameter => None,
855        FilesetParseErrorKind::Expression(_) => find_source_parse_error_hint(&err),
856        FilesetParseErrorKind::InAliasExpansion(_)
857        | FilesetParseErrorKind::InParameterExpansion(_)
858        | FilesetParseErrorKind::RecursiveAlias(_) => None,
859    }
860}
861
862fn opset_resolution_error_hint(err: &OpsetResolutionError) -> Option<String> {
863    match err {
864        OpsetResolutionError::MultipleOperations {
865            expr: _,
866            candidates,
867        } => Some(format!(
868            "Try specifying one of the operations by ID: {}",
869            candidates.iter().map(short_operation_hash).join(", ")
870        )),
871        OpsetResolutionError::EmptyOperations(_)
872        | OpsetResolutionError::InvalidIdPrefix(_)
873        | OpsetResolutionError::NoSuchOperation(_)
874        | OpsetResolutionError::AmbiguousIdPrefix(_) => None,
875    }
876}
877
878pub(crate) fn revset_parse_error_hint(err: &RevsetParseError) -> Option<String> {
879    // Only for the bottom error, which is usually the root cause
880    let bottom_err = iter::successors(Some(err), |e| e.origin()).last().unwrap();
881    match bottom_err.kind() {
882        RevsetParseErrorKind::SyntaxError => Some(
883            "See https://docs.jj-vcs.dev/latest/revsets/ or use `jj help -k revsets` for revsets \
884             syntax and how to quote symbols."
885                .into(),
886        ),
887        RevsetParseErrorKind::NotPrefixOperator {
888            op: _,
889            similar_op,
890            description,
891        }
892        | RevsetParseErrorKind::NotPostfixOperator {
893            op: _,
894            similar_op,
895            description,
896        }
897        | RevsetParseErrorKind::NotInfixOperator {
898            op: _,
899            similar_op,
900            description,
901        } => Some(format!("Did you mean `{similar_op}` for {description}?")),
902        RevsetParseErrorKind::NoSuchFunction {
903            name: _,
904            candidates,
905        } => format_similarity_hint(candidates),
906        RevsetParseErrorKind::InvalidFunctionArguments { .. }
907        | RevsetParseErrorKind::Expression(_) => find_source_parse_error_hint(bottom_err),
908        _ => None,
909    }
910}
911
912fn revset_resolution_error_hints(err: &RevsetResolutionError) -> Vec<String> {
913    let multiple_targets_hint = |targets: &[CommitId]| {
914        format!(
915            "Use commit ID to select single revision from: {}",
916            targets.iter().map(|id| format!("{id:.12}")).join(", ")
917        )
918    };
919    match err {
920        RevsetResolutionError::NoSuchRevision {
921            name: _,
922            candidates,
923        } => format_similarity_hint(candidates).into_iter().collect(),
924        RevsetResolutionError::DivergentChangeId {
925            symbol,
926            visible_targets,
927        } => vec![
928            format!(
929                "Use change offset to select single revision: {}",
930                visible_targets
931                    .iter()
932                    .map(|(offset, _)| format!("{symbol}/{offset}"))
933                    .join(", ")
934            ),
935            format!("Use `change_id({symbol})` to select all revisions"),
936            "To abandon unneeded revisions, run `jj abandon <commit_id>`".to_owned(),
937        ],
938        RevsetResolutionError::ConflictedRef {
939            kind: "bookmark",
940            symbol,
941            targets,
942        } => vec![
943            multiple_targets_hint(targets),
944            format!("Use `bookmarks({symbol})` to select all revisions"),
945            format!(
946                "To set which revision the bookmark points to, run `jj bookmark set {symbol} -r \
947                 <REVISION>`"
948            ),
949        ],
950        RevsetResolutionError::ConflictedRef {
951            kind: _,
952            symbol: _,
953            targets,
954        } => vec![multiple_targets_hint(targets)],
955        RevsetResolutionError::EmptyString
956        | RevsetResolutionError::WorkspaceMissingWorkingCopy { .. }
957        | RevsetResolutionError::AmbiguousCommitIdPrefix(_)
958        | RevsetResolutionError::AmbiguousChangeIdPrefix(_)
959        | RevsetResolutionError::Backend(_)
960        | RevsetResolutionError::Other(_) => vec![],
961    }
962}
963
964fn string_pattern_parse_error_hint(err: &StringPatternParseError) -> Option<String> {
965    match err {
966        StringPatternParseError::InvalidKind(_) => Some(
967            "Try prefixing with one of `exact:`, `glob:`, `regex:`, `substring:`, or one of these \
968             with `-i` suffix added (e.g. `glob-i:`) for case-insensitive matching"
969                .into(),
970        ),
971        StringPatternParseError::GlobPattern(_) | StringPatternParseError::Regex(_) => None,
972    }
973}
974
975fn tag_name_parse_error_hint(_: &TagNameParseError) -> Option<String> {
976    Some(REVSET_SYMBOL_HINT.to_owned())
977}
978
979fn template_parse_error_hint(err: &TemplateParseError) -> Option<String> {
980    // Only for the bottom error, which is usually the root cause
981    let bottom_err = iter::successors(Some(err), |e| e.origin()).last().unwrap();
982    match bottom_err.kind() {
983        TemplateParseErrorKind::NoSuchKeyword { candidates, .. }
984        | TemplateParseErrorKind::NoSuchFunction { candidates, .. }
985        | TemplateParseErrorKind::NoSuchMethod { candidates, .. } => {
986            format_similarity_hint(candidates)
987        }
988        TemplateParseErrorKind::InvalidArguments { .. } | TemplateParseErrorKind::Expression(_) => {
989            find_source_parse_error_hint(bottom_err)
990        }
991        _ => None,
992    }
993}
994
995const BROKEN_PIPE_EXIT_CODE: u8 = 3;
996
997pub(crate) fn handle_command_result(ui: &mut Ui, result: Result<(), CommandError>) -> u8 {
998    try_handle_command_result(ui, result).unwrap_or(BROKEN_PIPE_EXIT_CODE)
999}
1000
1001fn try_handle_command_result(ui: &mut Ui, result: Result<(), CommandError>) -> io::Result<u8> {
1002    let Err(cmd_err) = &result else {
1003        return Ok(0);
1004    };
1005    let err = &cmd_err.error;
1006    let hints = &cmd_err.hints;
1007    match cmd_err.kind {
1008        CommandErrorKind::User => {
1009            print_error(ui, "Error: ", err, hints)?;
1010            Ok(1)
1011        }
1012        CommandErrorKind::Config => {
1013            print_error(ui, "Config error: ", err, hints)?;
1014            writeln!(
1015                ui.stderr_formatter().labeled("hint"),
1016                "For help, see https://docs.jj-vcs.dev/latest/config/ or use `jj help -k config`."
1017            )?;
1018            Ok(1)
1019        }
1020        CommandErrorKind::Cli => {
1021            if let Some(err) = err.downcast_ref::<clap::Error>() {
1022                handle_clap_error(ui, err, hints)
1023            } else {
1024                print_error(ui, "Error: ", err, hints)?;
1025                Ok(2)
1026            }
1027        }
1028        CommandErrorKind::BrokenPipe => {
1029            // A broken pipe is not an error, but a signal to exit gracefully.
1030            Ok(BROKEN_PIPE_EXIT_CODE)
1031        }
1032        CommandErrorKind::Internal => {
1033            print_error(ui, "Internal error: ", err, hints)?;
1034            Ok(255)
1035        }
1036    }
1037}
1038
1039fn print_error(
1040    ui: &Ui,
1041    heading: &str,
1042    err: &dyn error::Error,
1043    hints: &[ErrorHint],
1044) -> io::Result<()> {
1045    writeln!(ui.error_with_heading(heading), "{err}")?;
1046    print_error_sources(ui, err.source())?;
1047    print_error_hints(ui, hints)?;
1048    Ok(())
1049}
1050
1051/// Prints error sources one by one from the given `source` inclusive.
1052pub fn print_error_sources(ui: &Ui, source: Option<&dyn error::Error>) -> io::Result<()> {
1053    let Some(err) = source else {
1054        return Ok(());
1055    };
1056    let mut formatter = ui.stderr_formatter().into_labeled("error_source");
1057    if err.source().is_none() {
1058        write!(formatter.labeled("heading"), "Caused by: ")?;
1059        writeln!(formatter, "{err}")?;
1060    } else {
1061        writeln!(formatter.labeled("heading"), "Caused by:")?;
1062        for (i, err) in iter::successors(Some(err), |&err| err.source()).enumerate() {
1063            write!(formatter.labeled("heading"), "{}: ", i + 1)?;
1064            writeln!(formatter, "{err}")?;
1065        }
1066    }
1067    Ok(())
1068}
1069
1070fn print_error_hints(ui: &Ui, hints: &[ErrorHint]) -> io::Result<()> {
1071    let mut formatter = ui.stderr_formatter().into_labeled("hint");
1072    for hint in hints {
1073        write!(formatter.labeled("heading"), "Hint: ")?;
1074        match hint {
1075            ErrorHint::PlainText(message) => {
1076                writeln!(formatter, "{message}")?;
1077            }
1078            ErrorHint::Formatted(recorded) => {
1079                recorded.replay(formatter.as_mut())?;
1080                // Formatted hint is usually multi-line text, and it's
1081                // convenient if trailing "\n" doesn't have to be omitted.
1082                if !recorded.data().ends_with(b"\n") {
1083                    writeln!(formatter)?;
1084                }
1085            }
1086        }
1087    }
1088    Ok(())
1089}
1090
1091fn handle_clap_error(ui: &mut Ui, err: &clap::Error, hints: &[ErrorHint]) -> io::Result<u8> {
1092    let clap_str = if ui.color() {
1093        err.render().ansi().to_string()
1094    } else {
1095        err.render().to_string()
1096    };
1097
1098    match err.kind() {
1099        clap::error::ErrorKind::DisplayHelp
1100        | clap::error::ErrorKind::DisplayHelpOnMissingArgumentOrSubcommand => ui.request_pager(),
1101        _ => {}
1102    }
1103    // Definitions for exit codes and streams come from
1104    // https://github.com/clap-rs/clap/blob/master/src/error/mod.rs
1105    match err.kind() {
1106        clap::error::ErrorKind::DisplayHelp | clap::error::ErrorKind::DisplayVersion => {
1107            write!(ui.stdout(), "{clap_str}")?;
1108            return Ok(0);
1109        }
1110        _ => {}
1111    }
1112    write!(ui.stderr(), "{clap_str}")?;
1113    // Skip the first source error, which should be printed inline.
1114    print_error_sources(ui, err.source().and_then(|err| err.source()))?;
1115    print_error_hints(ui, hints)?;
1116    Ok(2)
1117}
1118
1119/// Prints diagnostic messages emitted during parsing.
1120pub fn print_parse_diagnostics<T: error::Error>(
1121    ui: &Ui,
1122    context_message: &str,
1123    diagnostics: &Diagnostics<T>,
1124) -> io::Result<()> {
1125    for diag in diagnostics {
1126        writeln!(ui.warning_default(), "{context_message}")?;
1127        for err in iter::successors(Some(diag as &dyn error::Error), |&err| err.source()) {
1128            writeln!(ui.stderr(), "{err}")?;
1129        }
1130        // If we add support for multiple error diagnostics, we might have to do
1131        // find_source_parse_error_hint() and print it here.
1132    }
1133    Ok(())
1134}