| 1 |
use std::convert::From; |
| 2 |
|
| 3 |
use format_bytes::format_bytes; |
| 4 |
use hg::config::ConfigError; |
| 5 |
use hg::config::ConfigParseError; |
| 6 |
use hg::config::ConfigValueParseError; |
| 7 |
use hg::dirstate::on_disk::DirstateV2ParseError; |
| 8 |
use hg::dirstate::status::StatusError; |
| 9 |
use hg::dirstate::DirstateError; |
| 10 |
use hg::dirstate::DirstateMapError; |
| 11 |
use hg::errors::HgBacktrace; |
| 12 |
use hg::errors::HgError; |
| 13 |
use hg::exit_codes; |
| 14 |
use hg::file_patterns::PatternError; |
| 15 |
use hg::narrow::shape; |
| 16 |
use hg::repo::RepoError; |
| 17 |
use hg::revlog::RevlogError; |
| 18 |
use hg::sparse::SparseConfigError; |
| 19 |
use hg::utils::files::get_bytes_from_path; |
| 20 |
use hg::utils::hg_path::HgPathError; |
| 21 |
|
| 22 |
use crate::ui::UiError; |
| 23 |
use crate::NoRepoInCwdError; |
| 24 |
|
| 25 |
/// The kind of command error |
| 26 |
#[derive(Debug)] |
| 27 |
pub enum CommandError { |
| 28 |
/// Exit with an error message and "standard" failure exit code. |
| 29 |
Abort { |
| 30 |
message: Vec<u8>, |
| 31 |
detailed_exit_code: exit_codes::ExitCode, |
| 32 |
hint: Option<Vec<u8>>, |
| 33 |
backtrace: HgBacktrace, |
| 34 |
}, |
| 35 |
|
| 36 |
/// Exit with a failure exit code but no message. |
| 37 |
Unsuccessful, |
| 38 |
|
| 39 |
/// Encountered something (such as a CLI argument, repository layout, …) |
| 40 |
/// not supported by this version of `rhg`. Depending on configuration |
| 41 |
/// `rhg` may attempt to silently fall back to Python-based `hg`, which |
| 42 |
/// may or may not support this feature. |
| 43 |
UnsupportedFeature { message: Vec<u8> }, |
| 44 |
/// The fallback executable does not exist (or has some other problem if |
| 45 |
/// we end up being more precise about broken fallbacks). |
| 46 |
InvalidFallback { path: Vec<u8>, err: String }, |
| 47 |
} |
| 48 |
|
| 49 |
impl CommandError { |
| 50 |
pub fn abort(message: impl AsRef<str>) -> Self { |
| 51 |
CommandError::abort_with_exit_code(message, exit_codes::ABORT) |
| 52 |
} |
| 53 |
|
| 54 |
pub fn abort_bytes(message: impl AsRef<[u8]>) -> Self { |
| 55 |
CommandError::abort_with_exit_code_bytes(message, exit_codes::ABORT) |
| 56 |
} |
| 57 |
|
| 58 |
pub fn abort_with_exit_code( |
| 59 |
message: impl AsRef<str>, |
| 60 |
detailed_exit_code: exit_codes::ExitCode, |
| 61 |
) -> Self { |
| 62 |
CommandError::Abort { |
| 63 |
// TODO: bytes-based (instead of Unicode-based) formatting |
| 64 |
// of error messages to handle non-UTF-8 filenames etc: |
| 65 |
// https://www.mercurial-scm.org/wiki/EncodingStrategy#Mixing_output |
| 66 |
message: message.as_ref().as_bytes().to_owned(), |
| 67 |
detailed_exit_code, |
| 68 |
hint: None, |
| 69 |
backtrace: HgBacktrace::capture(), |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
fn abort_with_exit_code_and_hint( |
| 74 |
message: impl AsRef<str>, |
| 75 |
detailed_exit_code: exit_codes::ExitCode, |
| 76 |
hint: Option<impl AsRef<str>>, |
| 77 |
backtrace: HgBacktrace, |
| 78 |
) -> Self { |
| 79 |
CommandError::Abort { |
| 80 |
message: message.as_ref().as_bytes().to_owned(), |
| 81 |
detailed_exit_code, |
| 82 |
hint: hint.map(|h| h.as_ref().as_bytes().to_owned()), |
| 83 |
backtrace, |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
pub fn abort_with_exit_code_bytes( |
| 88 |
message: impl AsRef<[u8]>, |
| 89 |
detailed_exit_code: exit_codes::ExitCode, |
| 90 |
) -> Self { |
| 91 |
// TODO: use this everywhere it makes sense instead of the string |
| 92 |
// version. |
| 93 |
CommandError::Abort { |
| 94 |
message: message.as_ref().into(), |
| 95 |
detailed_exit_code, |
| 96 |
hint: None, |
| 97 |
backtrace: HgBacktrace::capture(), |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
pub fn unsupported(message: impl AsRef<str>) -> Self { |
| 102 |
CommandError::UnsupportedFeature { |
| 103 |
message: message.as_ref().as_bytes().to_owned(), |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
/// For now we don’t differenciate between invalid CLI args and valid for `hg` |
| 109 |
/// but not supported yet by `rhg`. |
| 110 |
impl From<clap::Error> for CommandError { |
| 111 |
fn from(error: clap::Error) -> Self { |
| 112 |
CommandError::unsupported(error.to_string()) |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
impl From<HgError> for CommandError { |
| 117 |
fn from(error: HgError) -> Self { |
| 118 |
match error { |
| 119 |
HgError::UnsupportedFeature(message, backtrace) => { |
| 120 |
CommandError::unsupported(format!("{}{}", backtrace, message)) |
| 121 |
} |
| 122 |
e @ HgError::CensoredNodeError(_, _) => { |
| 123 |
CommandError::unsupported(format!("abort: {}", e)) |
| 124 |
} |
| 125 |
HgError::Abort { message, detailed_exit_code, hint, backtrace } => { |
| 126 |
CommandError::abort_with_exit_code_and_hint( |
| 127 |
message, |
| 128 |
detailed_exit_code, |
| 129 |
hint, |
| 130 |
backtrace, |
| 131 |
) |
| 132 |
} |
| 133 |
_ => CommandError::abort(error.to_string()), |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
impl From<shape::Error> for CommandError { |
| 139 |
fn from(value: shape::Error) -> Self { |
| 140 |
HgError::from(value).into() |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
impl From<ConfigValueParseError> for CommandError { |
| 145 |
fn from(error: ConfigValueParseError) -> Self { |
| 146 |
CommandError::abort_with_exit_code( |
| 147 |
error.to_string(), |
| 148 |
exit_codes::CONFIG_ERROR_ABORT, |
| 149 |
) |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
impl From<UiError> for CommandError { |
| 154 |
fn from(_error: UiError) -> Self { |
| 155 |
// If we already failed writing to stdout or stderr, |
| 156 |
// writing an error message to stderr about it would be likely to fail |
| 157 |
// too. |
| 158 |
CommandError::abort("") |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
impl From<RepoError> for CommandError { |
| 163 |
fn from(error: RepoError) -> Self { |
| 164 |
match error { |
| 165 |
RepoError::NotFound { at } => { |
| 166 |
CommandError::abort_with_exit_code_bytes( |
| 167 |
format_bytes!( |
| 168 |
b"abort: repository {} not found", |
| 169 |
get_bytes_from_path(at) |
| 170 |
), |
| 171 |
exit_codes::ABORT, |
| 172 |
) |
| 173 |
} |
| 174 |
RepoError::ConfigParseError(error) => error.into(), |
| 175 |
RepoError::Other(error) => error.into(), |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
impl<'a> From<&'a NoRepoInCwdError> for CommandError { |
| 181 |
fn from(error: &'a NoRepoInCwdError) -> Self { |
| 182 |
let NoRepoInCwdError { cwd } = error; |
| 183 |
CommandError::abort_with_exit_code_bytes( |
| 184 |
format_bytes!( |
| 185 |
b"abort: no repository found in '{}' (.hg not found)!", |
| 186 |
get_bytes_from_path(cwd) |
| 187 |
), |
| 188 |
exit_codes::ABORT, |
| 189 |
) |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
impl From<ConfigError> for CommandError { |
| 194 |
fn from(error: ConfigError) -> Self { |
| 195 |
match error { |
| 196 |
ConfigError::Parse(error) => error.into(), |
| 197 |
ConfigError::Other(error) => error.into(), |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
impl From<ConfigParseError> for CommandError { |
| 203 |
fn from(error: ConfigParseError) -> Self { |
| 204 |
let ConfigParseError { origin, line, message } = error; |
| 205 |
let line_message = if let Some(line_number) = line { |
| 206 |
format_bytes!(b":{}", line_number.to_string().into_bytes()) |
| 207 |
} else { |
| 208 |
Vec::new() |
| 209 |
}; |
| 210 |
CommandError::abort_with_exit_code_bytes( |
| 211 |
format_bytes!( |
| 212 |
b"config error at {}{}: {}", |
| 213 |
origin, |
| 214 |
line_message, |
| 215 |
message |
| 216 |
), |
| 217 |
exit_codes::CONFIG_ERROR_ABORT, |
| 218 |
) |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
impl From<RevlogError> for CommandError { |
| 223 |
fn from(err: RevlogError) -> CommandError { |
| 224 |
let err: HgError = err.into(); |
| 225 |
err.into() |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
impl From<StatusError> for CommandError { |
| 230 |
fn from(error: StatusError) -> Self { |
| 231 |
match error { |
| 232 |
StatusError::Pattern(_) => { |
| 233 |
CommandError::unsupported(format!("{}", error)) |
| 234 |
} |
| 235 |
_ => CommandError::abort(format!("{}", error)), |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
impl From<HgPathError> for CommandError { |
| 241 |
fn from(error: HgPathError) -> Self { |
| 242 |
CommandError::unsupported(format!("{}", error)) |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
impl From<PatternError> for CommandError { |
| 247 |
fn from(error: PatternError) -> Self { |
| 248 |
CommandError::unsupported(format!("{}", error)) |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
impl From<DirstateMapError> for CommandError { |
| 253 |
fn from(error: DirstateMapError) -> Self { |
| 254 |
CommandError::abort(format!("{}", error)) |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
impl From<DirstateError> for CommandError { |
| 259 |
fn from(error: DirstateError) -> Self { |
| 260 |
match error { |
| 261 |
DirstateError::Common(error) => error.into(), |
| 262 |
DirstateError::Map(error) => error.into(), |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
impl From<DirstateV2ParseError> for CommandError { |
| 268 |
fn from(error: DirstateV2ParseError) -> Self { |
| 269 |
HgError::from(error).into() |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
impl From<SparseConfigError> for CommandError { |
| 274 |
fn from(e: SparseConfigError) -> Self { |
| 275 |
match e { |
| 276 |
SparseConfigError::IncludesAfterExcludes { context } => { |
| 277 |
Self::abort_with_exit_code_bytes( |
| 278 |
format_bytes!( |
| 279 |
b"{} config cannot have includes after excludes", |
| 280 |
context |
| 281 |
), |
| 282 |
exit_codes::CONFIG_PARSE_ERROR_ABORT, |
| 283 |
) |
| 284 |
} |
| 285 |
SparseConfigError::EntryOutsideSection { context, line } => { |
| 286 |
Self::abort_with_exit_code_bytes( |
| 287 |
format_bytes!( |
| 288 |
b"{} config entry outside of section: {}", |
| 289 |
context, |
| 290 |
&line, |
| 291 |
), |
| 292 |
exit_codes::CONFIG_PARSE_ERROR_ABORT, |
| 293 |
) |
| 294 |
} |
| 295 |
SparseConfigError::InvalidNarrowPrefix(prefix) => { |
| 296 |
Self::abort_with_exit_code_bytes( |
| 297 |
format_bytes!( |
| 298 |
b"invalid prefix on narrow pattern: {}", |
| 299 |
&prefix |
| 300 |
), |
| 301 |
exit_codes::ABORT, |
| 302 |
) |
| 303 |
} |
| 304 |
SparseConfigError::WhitespaceAtEdgeOfPattern(prefix) => { |
| 305 |
Self::abort_with_exit_code_bytes( |
| 306 |
format_bytes!( |
| 307 |
b"narrow pattern with whitespace at the edge: {}", |
| 308 |
&prefix |
| 309 |
), |
| 310 |
exit_codes::ABORT, |
| 311 |
) |
| 312 |
} |
| 313 |
SparseConfigError::IncludesInNarrow => Self::abort( |
| 314 |
"including other spec files using '%include' \ |
| 315 |
is not supported in narrowspec", |
| 316 |
), |
| 317 |
SparseConfigError::HgError(e) => Self::from(e), |
| 318 |
SparseConfigError::PatternError(e) => { |
| 319 |
Self::unsupported(format!("{}", e)) |
| 320 |
} |
| 321 |
} |
| 322 |
} |
| 323 |
} |