2018-12-11 16:43:26 +02:00
|
|
|
use failure::{Backtrace, Context, Fail};
|
|
|
|
|
use std::fmt::{self, Display};
|
|
|
|
|
use std::process::ExitStatus;
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct Error {
|
|
|
|
|
inner: Context<ErrorKind>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Eq, PartialEq, Debug, Fail)]
|
|
|
|
|
pub enum ErrorKind {
|
|
|
|
|
#[fail(display = "Error asking the user for retry")]
|
|
|
|
|
Retry,
|
|
|
|
|
|
|
|
|
|
#[fail(display = "Cannot find the user base directories")]
|
|
|
|
|
NoBaseDirectories,
|
|
|
|
|
|
|
|
|
|
#[fail(display = "A step failed")]
|
|
|
|
|
StepFailed,
|
|
|
|
|
|
|
|
|
|
#[fail(display = "Error reading the configuration")]
|
|
|
|
|
Configuration,
|
|
|
|
|
|
|
|
|
|
#[fail(display = "A custom pre-command failed")]
|
|
|
|
|
PreCommand,
|
|
|
|
|
|
|
|
|
|
#[fail(display = "{}", _0)]
|
|
|
|
|
ProcessFailed(ExitStatus),
|
|
|
|
|
|
|
|
|
|
#[fail(display = "Unknown Linux Distribution")]
|
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
|
UnknownLinuxDistribution,
|
|
|
|
|
|
|
|
|
|
#[fail(display = "Process execution failure")]
|
|
|
|
|
ProcessExecution,
|
|
|
|
|
|
|
|
|
|
#[fail(display = "Self-update failure")]
|
|
|
|
|
#[cfg(feature = "self-update")]
|
|
|
|
|
SelfUpdate,
|
2019-01-13 23:20:32 +02:00
|
|
|
|
|
|
|
|
#[fail(display = "A step should be skipped")]
|
|
|
|
|
SkipStep,
|
2019-06-03 09:41:25 +03:00
|
|
|
|
|
|
|
|
#[cfg(all(windows, feature = "self-update"))]
|
|
|
|
|
#[fail(display = "Topgrade Upgraded")]
|
|
|
|
|
Upgraded(ExitStatus),
|
2018-12-11 16:43:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Fail for Error {
|
2019-08-17 20:58:45 +03:00
|
|
|
fn cause(&self) -> Option<&dyn Fail> {
|
2018-12-11 16:43:26 +02:00
|
|
|
self.inner.cause()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn backtrace(&self) -> Option<&Backtrace> {
|
|
|
|
|
self.inner.backtrace()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Display for Error {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
Display::fmt(&self.inner, f)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Error {
|
|
|
|
|
pub fn kind(&self) -> ErrorKind {
|
|
|
|
|
*self.inner.get_context()
|
|
|
|
|
}
|
2019-06-03 09:41:25 +03:00
|
|
|
|
|
|
|
|
#[cfg(all(windows, feature = "self-update"))]
|
|
|
|
|
pub fn upgraded(&self) -> bool {
|
|
|
|
|
if let ErrorKind::Upgraded(_) = self.kind() {
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-11 16:43:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<ErrorKind> for Error {
|
|
|
|
|
fn from(kind: ErrorKind) -> Error {
|
|
|
|
|
Error {
|
|
|
|
|
inner: Context::new(kind),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<Context<ErrorKind>> for Error {
|
|
|
|
|
fn from(inner: Context<ErrorKind>) -> Error {
|
|
|
|
|
Error { inner }
|
|
|
|
|
}
|
|
|
|
|
}
|