Skip to main content

jj_cz/
error.rs

1use jj_lib::revset::{RevsetEvaluationError, RevsetParseError, RevsetResolutionError};
2
3use crate::commit::types::{CommitMessageError, DescriptionError, ScopeError};
4
5#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
6pub enum Error {
7    // Domain errors
8    #[error("Invalid scope: {0}")]
9    InvalidScope(String),
10    #[error("Invalid description: {0}")]
11    InvalidDescription(String),
12    #[error("Invalid commit message: {0}")]
13    InvalidCommitMessage(String),
14    // Infrastructure errors
15    #[error("Not a Jujutsu repository")]
16    NotARepository,
17    #[error("Repository operation failed: {context}")]
18    JjOperation { context: String },
19    #[error("Repository is locked by another process")]
20    RepositoryLocked,
21    #[error("Could not get current directory")]
22    FailedGettingCurrentDir,
23    #[error("Could not load Jujutsu configuration: {context}")]
24    FailedReadingConfig { context: String },
25    // Application errors
26    #[error("Operation cancelled by user")]
27    Cancelled,
28    #[error("Non-interactive terminal detected")]
29    NonInteractive,
30    #[error("Failed to resolve revision '{revset}': {context}")]
31    RevsetResolutionError { revset: String, context: String },
32    #[error("Revision set '{revset}' resolves to multiple commits; specify a single revision")]
33    MultipleRevisions { revset: String },
34    #[error("--new cannot be used with multiple revisions")]
35    NewFlagWithMultipleRevisions,
36}
37
38impl From<ScopeError> for Error {
39    fn from(value: ScopeError) -> Self {
40        Self::InvalidScope(value.to_string())
41    }
42}
43
44impl From<DescriptionError> for Error {
45    fn from(value: DescriptionError) -> Self {
46        Self::InvalidDescription(value.to_string())
47    }
48}
49
50impl From<CommitMessageError> for Error {
51    fn from(value: CommitMessageError) -> Self {
52        Self::InvalidCommitMessage(value.to_string())
53    }
54}
55
56impl From<std::io::Error> for Error {
57    fn from(_: std::io::Error) -> Self {
58        Self::FailedGettingCurrentDir
59    }
60}
61
62impl<T> From<std::sync::PoisonError<T>> for Error {
63    fn from(_: std::sync::PoisonError<T>) -> Self {
64        Self::JjOperation {
65            context: "internal lock poisoned".to_string(),
66        }
67    }
68}
69
70impl Error {
71    pub fn from_revset_parse_error(revset: &str, error: RevsetParseError) -> Self {
72        Self::RevsetResolutionError {
73            revset: revset.to_string(),
74            context: error.to_string(),
75        }
76    }
77
78    pub fn from_revset_resolution_error(revset: &str, error: RevsetResolutionError) -> Self {
79        Self::RevsetResolutionError {
80            revset: revset.to_string(),
81            context: error.to_string(),
82        }
83    }
84
85    pub fn from_revset_evaluation_error(revset: &str, error: RevsetEvaluationError) -> Self {
86        Self::RevsetResolutionError {
87            revset: revset.to_string(),
88            context: error.to_string(),
89        }
90    }
91}