1use 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 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 pub fn hinted(mut self, hint: impl Into<String>) -> Self {
126 self.add_hint(hint);
127 self
128 }
129
130 pub fn add_hint(&mut self, hint: impl Into<String>) {
132 self.hints.push(ErrorHint::PlainText(hint.into()));
133 }
134
135 pub fn add_formatted_hint(&mut self, hint: FormatRecorder) {
137 self.hints.push(ErrorHint::Formatted(hint));
138 }
139
140 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 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#[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::RevsetEvaluationError(_) => user_error(err),
761 }
762 }
763}
764
765impl From<SecureConfigError> for CommandError {
766 fn from(err: SecureConfigError) -> Self {
767 internal_error_with_message("Failed to determine the secure config for a repo", err)
768 }
769}
770
771fn find_source_parse_error_hint(err: &dyn error::Error) -> Option<String> {
772 let source = err.source()?;
773 if let Some(source) = source.downcast_ref() {
774 bookmark_name_parse_error_hint(source)
775 } else if let Some(source) = source.downcast_ref() {
776 config_get_error_hint(source)
777 } else if let Some(source) = source.downcast_ref() {
778 file_pattern_parse_error_hint(source)
779 } else if let Some(source) = source.downcast_ref() {
780 fileset_parse_error_hint(source)
781 } else if let Some(source) = source.downcast_ref() {
782 revset_parse_error_hint(source)
783 } else if let Some(source) = source.downcast_ref() {
784 revset_resolution_error_hints(source).into_iter().next()
786 } else if let Some(UserRevsetEvaluationError::Resolution(source)) = source.downcast_ref() {
787 revset_resolution_error_hints(source).into_iter().next()
789 } else if let Some(source) = source.downcast_ref() {
790 string_pattern_parse_error_hint(source)
791 } else if let Some(source) = source.downcast_ref() {
792 tag_name_parse_error_hint(source)
793 } else if let Some(source) = source.downcast_ref() {
794 template_parse_error_hint(source)
795 } else {
796 None
797 }
798}
799
800const REVSET_SYMBOL_HINT: &str = "See https://docs.jj-vcs.dev/latest/revsets/ or use `jj help -k \
801 revsets` for how to quote symbols.";
802
803fn bookmark_name_parse_error_hint(err: &BookmarkNameParseError) -> Option<String> {
804 use revset::ExpressionKind;
805 match revset::parse_program(&err.input).map(|node| node.kind) {
806 Ok(ExpressionKind::RemoteSymbol(symbol)) => Some(format!(
807 "Looks like remote bookmark. Run `jj bookmark track {name} --remote={remote}` to \
808 track it.",
809 name = symbol.name.as_symbol(),
810 remote = symbol.remote.as_symbol()
811 )),
812 _ => Some(REVSET_SYMBOL_HINT.to_owned()),
813 }
814}
815
816fn config_get_error_hint(err: &ConfigGetError) -> Option<String> {
817 match &err {
818 ConfigGetError::NotFound { .. } => None,
819 ConfigGetError::Type { source_path, .. } => source_path
820 .as_ref()
821 .map(|path| format!("Check the config file: {}", path.display())),
822 }
823}
824
825fn file_pattern_parse_error_hint(err: &FilePatternParseError) -> Option<String> {
826 match err {
827 FilePatternParseError::InvalidKind(_) => Some(String::from(
828 "See https://docs.jj-vcs.dev/latest/filesets/#file-patterns or `jj help -k filesets` \
829 for valid prefixes.",
830 )),
831 FilePatternParseError::UiPath(UiPathParseError::Fs(e)) => {
833 RepoPathBuf::from_relative_path(&e.input).ok().map(|path| {
834 format!(r#"Consider using root:{path:?} to specify repo-relative path"#)
835 })
836 }
837 FilePatternParseError::RelativePath(_) => None,
838 FilePatternParseError::GlobPattern(_) => None,
839 }
840}
841
842fn fileset_parse_error_hint(err: &FilesetParseError) -> Option<String> {
843 match err.kind() {
844 FilesetParseErrorKind::SyntaxError => Some(String::from(
845 "See https://docs.jj-vcs.dev/latest/filesets/ or use `jj help -k filesets` for \
846 filesets syntax and how to match file paths.",
847 )),
848 FilesetParseErrorKind::NoSuchFunction {
849 name: _,
850 candidates,
851 } => format_similarity_hint(candidates),
852 FilesetParseErrorKind::InvalidArguments { .. } | FilesetParseErrorKind::Expression(_) => {
853 find_source_parse_error_hint(&err)
854 }
855 }
856}
857
858fn opset_resolution_error_hint(err: &OpsetResolutionError) -> Option<String> {
859 match err {
860 OpsetResolutionError::MultipleOperations {
861 expr: _,
862 candidates,
863 } => Some(format!(
864 "Try specifying one of the operations by ID: {}",
865 candidates.iter().map(short_operation_hash).join(", ")
866 )),
867 OpsetResolutionError::EmptyOperations(_)
868 | OpsetResolutionError::InvalidIdPrefix(_)
869 | OpsetResolutionError::NoSuchOperation(_)
870 | OpsetResolutionError::AmbiguousIdPrefix(_) => None,
871 }
872}
873
874pub(crate) fn revset_parse_error_hint(err: &RevsetParseError) -> Option<String> {
875 let bottom_err = iter::successors(Some(err), |e| e.origin()).last().unwrap();
877 match bottom_err.kind() {
878 RevsetParseErrorKind::SyntaxError => Some(
879 "See https://docs.jj-vcs.dev/latest/revsets/ or use `jj help -k revsets` for revsets \
880 syntax and how to quote symbols."
881 .into(),
882 ),
883 RevsetParseErrorKind::NotPrefixOperator {
884 op: _,
885 similar_op,
886 description,
887 }
888 | RevsetParseErrorKind::NotPostfixOperator {
889 op: _,
890 similar_op,
891 description,
892 }
893 | RevsetParseErrorKind::NotInfixOperator {
894 op: _,
895 similar_op,
896 description,
897 } => Some(format!("Did you mean `{similar_op}` for {description}?")),
898 RevsetParseErrorKind::NoSuchFunction {
899 name: _,
900 candidates,
901 } => format_similarity_hint(candidates),
902 RevsetParseErrorKind::InvalidFunctionArguments { .. }
903 | RevsetParseErrorKind::Expression(_) => find_source_parse_error_hint(bottom_err),
904 _ => None,
905 }
906}
907
908fn revset_resolution_error_hints(err: &RevsetResolutionError) -> Vec<String> {
909 let multiple_targets_hint = |targets: &[CommitId]| {
910 format!(
911 "Use commit ID to select single revision from: {}",
912 targets.iter().map(|id| format!("{id:.12}")).join(", ")
913 )
914 };
915 match err {
916 RevsetResolutionError::NoSuchRevision {
917 name: _,
918 candidates,
919 } => format_similarity_hint(candidates).into_iter().collect(),
920 RevsetResolutionError::DivergentChangeId {
921 symbol,
922 visible_targets,
923 } => vec![
924 format!(
925 "Use change offset to select single revision: {}",
926 visible_targets
927 .iter()
928 .map(|(offset, _)| format!("{symbol}/{offset}"))
929 .join(", ")
930 ),
931 format!("Use `change_id({symbol})` to select all revisions"),
932 "To abandon unneeded revisions, run `jj abandon <commit_id>`".to_owned(),
933 ],
934 RevsetResolutionError::ConflictedRef {
935 kind: "bookmark",
936 symbol,
937 targets,
938 } => vec![
939 multiple_targets_hint(targets),
940 format!("Use `bookmarks({symbol})` to select all revisions"),
941 format!(
942 "To set which revision the bookmark points to, run `jj bookmark set {symbol} -r \
943 <REVISION>`"
944 ),
945 ],
946 RevsetResolutionError::ConflictedRef {
947 kind: _,
948 symbol: _,
949 targets,
950 } => vec![multiple_targets_hint(targets)],
951 RevsetResolutionError::EmptyString
952 | RevsetResolutionError::WorkspaceMissingWorkingCopy { .. }
953 | RevsetResolutionError::AmbiguousCommitIdPrefix(_)
954 | RevsetResolutionError::AmbiguousChangeIdPrefix(_)
955 | RevsetResolutionError::Backend(_)
956 | RevsetResolutionError::Other(_) => vec![],
957 }
958}
959
960fn string_pattern_parse_error_hint(err: &StringPatternParseError) -> Option<String> {
961 match err {
962 StringPatternParseError::InvalidKind(_) => Some(
963 "Try prefixing with one of `exact:`, `glob:`, `regex:`, `substring:`, or one of these \
964 with `-i` suffix added (e.g. `glob-i:`) for case-insensitive matching"
965 .into(),
966 ),
967 StringPatternParseError::GlobPattern(_) | StringPatternParseError::Regex(_) => None,
968 }
969}
970
971fn tag_name_parse_error_hint(_: &TagNameParseError) -> Option<String> {
972 Some(REVSET_SYMBOL_HINT.to_owned())
973}
974
975fn template_parse_error_hint(err: &TemplateParseError) -> Option<String> {
976 let bottom_err = iter::successors(Some(err), |e| e.origin()).last().unwrap();
978 match bottom_err.kind() {
979 TemplateParseErrorKind::NoSuchKeyword { candidates, .. }
980 | TemplateParseErrorKind::NoSuchFunction { candidates, .. }
981 | TemplateParseErrorKind::NoSuchMethod { candidates, .. } => {
982 format_similarity_hint(candidates)
983 }
984 TemplateParseErrorKind::InvalidArguments { .. } | TemplateParseErrorKind::Expression(_) => {
985 find_source_parse_error_hint(bottom_err)
986 }
987 _ => None,
988 }
989}
990
991const BROKEN_PIPE_EXIT_CODE: u8 = 3;
992
993pub(crate) fn handle_command_result(ui: &mut Ui, result: Result<(), CommandError>) -> u8 {
994 try_handle_command_result(ui, result).unwrap_or(BROKEN_PIPE_EXIT_CODE)
995}
996
997fn try_handle_command_result(ui: &mut Ui, result: Result<(), CommandError>) -> io::Result<u8> {
998 let Err(cmd_err) = &result else {
999 return Ok(0);
1000 };
1001 let err = &cmd_err.error;
1002 let hints = &cmd_err.hints;
1003 match cmd_err.kind {
1004 CommandErrorKind::User => {
1005 print_error(ui, "Error: ", err, hints)?;
1006 Ok(1)
1007 }
1008 CommandErrorKind::Config => {
1009 print_error(ui, "Config error: ", err, hints)?;
1010 writeln!(
1011 ui.stderr_formatter().labeled("hint"),
1012 "For help, see https://docs.jj-vcs.dev/latest/config/ or use `jj help -k config`."
1013 )?;
1014 Ok(1)
1015 }
1016 CommandErrorKind::Cli => {
1017 if let Some(err) = err.downcast_ref::<clap::Error>() {
1018 handle_clap_error(ui, err, hints)
1019 } else {
1020 print_error(ui, "Error: ", err, hints)?;
1021 Ok(2)
1022 }
1023 }
1024 CommandErrorKind::BrokenPipe => {
1025 Ok(BROKEN_PIPE_EXIT_CODE)
1027 }
1028 CommandErrorKind::Internal => {
1029 print_error(ui, "Internal error: ", err, hints)?;
1030 Ok(255)
1031 }
1032 }
1033}
1034
1035fn print_error(
1036 ui: &Ui,
1037 heading: &str,
1038 err: &dyn error::Error,
1039 hints: &[ErrorHint],
1040) -> io::Result<()> {
1041 writeln!(ui.error_with_heading(heading), "{err}")?;
1042 print_error_sources(ui, err.source())?;
1043 print_error_hints(ui, hints)?;
1044 Ok(())
1045}
1046
1047pub fn print_error_sources(ui: &Ui, source: Option<&dyn error::Error>) -> io::Result<()> {
1049 let Some(err) = source else {
1050 return Ok(());
1051 };
1052 let mut formatter = ui.stderr_formatter().into_labeled("error_source");
1053 if err.source().is_none() {
1054 write!(formatter.labeled("heading"), "Caused by: ")?;
1055 writeln!(formatter, "{err}")?;
1056 } else {
1057 writeln!(formatter.labeled("heading"), "Caused by:")?;
1058 for (i, err) in iter::successors(Some(err), |&err| err.source()).enumerate() {
1059 write!(formatter.labeled("heading"), "{}: ", i + 1)?;
1060 writeln!(formatter, "{err}")?;
1061 }
1062 }
1063 Ok(())
1064}
1065
1066fn print_error_hints(ui: &Ui, hints: &[ErrorHint]) -> io::Result<()> {
1067 let mut formatter = ui.stderr_formatter().into_labeled("hint");
1068 for hint in hints {
1069 write!(formatter.labeled("heading"), "Hint: ")?;
1070 match hint {
1071 ErrorHint::PlainText(message) => {
1072 writeln!(formatter, "{message}")?;
1073 }
1074 ErrorHint::Formatted(recorded) => {
1075 recorded.replay(formatter.as_mut())?;
1076 if !recorded.data().ends_with(b"\n") {
1079 writeln!(formatter)?;
1080 }
1081 }
1082 }
1083 }
1084 Ok(())
1085}
1086
1087fn handle_clap_error(ui: &mut Ui, err: &clap::Error, hints: &[ErrorHint]) -> io::Result<u8> {
1088 let clap_str = if ui.color() {
1089 err.render().ansi().to_string()
1090 } else {
1091 err.render().to_string()
1092 };
1093
1094 match err.kind() {
1095 clap::error::ErrorKind::DisplayHelp
1096 | clap::error::ErrorKind::DisplayHelpOnMissingArgumentOrSubcommand => ui.request_pager(),
1097 _ => {}
1098 }
1099 match err.kind() {
1102 clap::error::ErrorKind::DisplayHelp | clap::error::ErrorKind::DisplayVersion => {
1103 write!(ui.stdout(), "{clap_str}")?;
1104 return Ok(0);
1105 }
1106 _ => {}
1107 }
1108 write!(ui.stderr(), "{clap_str}")?;
1109 print_error_sources(ui, err.source().and_then(|err| err.source()))?;
1111 print_error_hints(ui, hints)?;
1112 Ok(2)
1113}
1114
1115pub fn print_parse_diagnostics<T: error::Error>(
1117 ui: &Ui,
1118 context_message: &str,
1119 diagnostics: &Diagnostics<T>,
1120) -> io::Result<()> {
1121 for diag in diagnostics {
1122 writeln!(ui.warning_default(), "{context_message}")?;
1123 for err in iter::successors(Some(diag as &dyn error::Error), |&err| err.source()) {
1124 writeln!(ui.stderr(), "{err}")?;
1125 }
1126 }
1129 Ok(())
1130}