error.rs 5.9 KB
Newer Older
1
use crate::exitcode;
2
use crate::ui::utf8_to_local;
3
use crate::ui::UiError;
4
use crate::NoRepoInCwdError;
5
use format_bytes::format_bytes;
6
use hg::config::{ConfigError, ConfigParseError, ConfigValueParseError};
7
use hg::errors::HgError;
8
use hg::repo::RepoError;
9
use hg::revlog::revlog::RevlogError;
10
use hg::utils::files::get_bytes_from_path;
11
use hg::{DirstateError, DirstateMapError, StatusError};
12
13
14
use std::convert::From;

/// The kind of command error
15
#[derive(Debug)]
16
pub enum CommandError {
17
    /// Exit with an error message and "standard" failure exit code.
18
19
20
21
    Abort {
        message: Vec<u8>,
        detailed_exit_code: exitcode::ExitCode,
    },
22

23
24
25
    /// Exit with a failure exit code but no message.
    Unsuccessful,

26
27
28
29
30
    /// Encountered something (such as a CLI argument, repository layout, …)
    /// not supported by this version of `rhg`. Depending on configuration
    /// `rhg` may attempt to silently fall back to Python-based `hg`, which
    /// may or may not support this feature.
    UnsupportedFeature { message: Vec<u8> },
31
32
}

33
impl CommandError {
34
    pub fn abort(message: impl AsRef<str>) -> Self {
35
36
37
38
39
40
41
        CommandError::abort_with_exit_code(message, exitcode::ABORT)
    }

    pub fn abort_with_exit_code(
        message: impl AsRef<str>,
        detailed_exit_code: exitcode::ExitCode,
    ) -> Self {
42
43
44
45
46
        CommandError::Abort {
            // TODO: bytes-based (instead of Unicode-based) formatting
            // of error messages to handle non-UTF-8 filenames etc:
            // https://www.mercurial-scm.org/wiki/EncodingStrategy#Mixing_output
            message: utf8_to_local(message.as_ref()).into(),
47
            detailed_exit_code: detailed_exit_code,
48
49
        }
    }
50
51
52
53
54
55

    pub fn unsupported(message: impl AsRef<str>) -> Self {
        CommandError::UnsupportedFeature {
            message: utf8_to_local(message.as_ref()).into(),
        }
    }
56
}
57

58
59
60
/// For now we don’t differenciate between invalid CLI args and valid for `hg`
/// but not supported yet by `rhg`.
impl From<clap::Error> for CommandError {
61
62
    fn from(error: clap::Error) -> Self {
        CommandError::unsupported(error.to_string())
63
64
65
    }
}

66
67
68
impl From<HgError> for CommandError {
    fn from(error: HgError) -> Self {
        match error {
69
70
71
            HgError::UnsupportedFeature(message) => {
                CommandError::unsupported(message)
            }
72
            _ => CommandError::abort(error.to_string()),
73
74
        }
    }
75
76
}

77
78
impl From<ConfigValueParseError> for CommandError {
    fn from(error: ConfigValueParseError) -> Self {
79
80
81
82
        CommandError::abort_with_exit_code(
            error.to_string(),
            exitcode::CONFIG_ERROR_ABORT,
        )
83
84
85
    }
}

86
impl From<UiError> for CommandError {
87
88
89
90
91
    fn from(_error: UiError) -> Self {
        // If we already failed writing to stdout or stderr,
        // writing an error message to stderr about it would be likely to fail
        // too.
        CommandError::abort("")
92
93
    }
}
94

95
96
impl From<RepoError> for CommandError {
    fn from(error: RepoError) -> Self {
97
        match error {
98
            RepoError::NotFound { at } => CommandError::Abort {
99
                message: format_bytes!(
100
                    b"abort: repository {} not found",
101
                    get_bytes_from_path(at)
102
                ),
103
                detailed_exit_code: exitcode::ABORT,
104
            },
105
106
            RepoError::ConfigParseError(error) => error.into(),
            RepoError::Other(error) => error.into(),
107
108
109
        }
    }
}
110

111
112
113
114
115
impl<'a> From<&'a NoRepoInCwdError> for CommandError {
    fn from(error: &'a NoRepoInCwdError) -> Self {
        let NoRepoInCwdError { cwd } = error;
        CommandError::Abort {
            message: format_bytes!(
116
                b"abort: no repository found in '{}' (.hg not found)!",
117
118
                get_bytes_from_path(cwd)
            ),
119
            detailed_exit_code: exitcode::ABORT,
120
121
122
123
        }
    }
}

124
125
126
impl From<ConfigError> for CommandError {
    fn from(error: ConfigError) -> Self {
        match error {
127
            ConfigError::Parse(error) => error.into(),
128
129
130
131
132
            ConfigError::Other(error) => error.into(),
        }
    }
}

133
134
135
136
137
impl From<ConfigParseError> for CommandError {
    fn from(error: ConfigParseError) -> Self {
        let ConfigParseError {
            origin,
            line,
138
            message,
139
140
        } = error;
        let line_message = if let Some(line_number) = line {
141
            format_bytes!(b":{}", line_number.to_string().into_bytes())
142
143
144
145
146
        } else {
            Vec::new()
        };
        CommandError::Abort {
            message: format_bytes!(
147
                b"config error at {}{}: {}",
148
                origin,
149
                line_message,
150
                message
151
            ),
152
            detailed_exit_code: exitcode::CONFIG_ERROR_ABORT,
153
154
155
156
        }
    }
}

157
158
impl From<(RevlogError, &str)> for CommandError {
    fn from((err, rev): (RevlogError, &str)) -> CommandError {
159
        match err {
160
161
162
            RevlogError::WDirUnsupported => CommandError::abort(
                "abort: working directory revision cannot be specified",
            ),
163
            RevlogError::InvalidRevision => CommandError::abort(format!(
164
                "abort: invalid revision identifier: {}",
165
                rev
166
            )),
167
            RevlogError::AmbiguousPrefix => CommandError::abort(format!(
168
                "abort: ambiguous revision identifier: {}",
169
                rev
170
            )),
171
            RevlogError::Other(error) => error.into(),
172
173
174
        }
    }
}
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195

impl From<StatusError> for CommandError {
    fn from(error: StatusError) -> Self {
        CommandError::abort(format!("{}", error))
    }
}

impl From<DirstateMapError> for CommandError {
    fn from(error: DirstateMapError) -> Self {
        CommandError::abort(format!("{}", error))
    }
}

impl From<DirstateError> for CommandError {
    fn from(error: DirstateError) -> Self {
        match error {
            DirstateError::Common(error) => error.into(),
            DirstateError::Map(error) => error.into(),
        }
    }
}