#[derive(Debug, thiserror::Error)]
#[error("failed to fetch source")]
pub struct FetchError {
source: crate::Source,
#[source]
err: FetchErrorKind,
}
impl FetchError {
pub(crate) fn new(err: FetchErrorKind, source: crate::Source) -> Self {
Self { source, err }
}
}
#[derive(Debug, thiserror::Error)]
pub(crate) enum FetchErrorKind {
#[error(transparent)]
Io(#[from] std::io::Error),
#[cfg(feature = "reqwest")]
#[error(transparent)]
Reqwest(#[from] reqwest::Error),
#[error("subprocess '{command}' exited with status {status}\n{stderr}")]
Subprocess {
command: String,
status: std::process::ExitStatus,
stderr: String,
},
}
impl FetchErrorKind {
pub fn subprocess(command: String, status: std::process::ExitStatus, stderr: String) -> Self {
Self::Subprocess {
command,
status,
stderr,
}
}
}
#[derive(Debug, thiserror::Error)]
pub struct Error {
kind: ErrorKind,
inner: ErrorImpl,
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.inner)
}
}
impl Error {
pub fn kind(&self) -> &ErrorKind {
&self.kind
}
}
#[derive(Debug, PartialEq, Eq)]
pub enum ErrorKind {
Io,
#[cfg(feature = "reqwest")]
Reqwest,
TomlDe,
SerdeDe,
Parse,
}
#[derive(Debug, thiserror::Error)]
pub(crate) enum ErrorImpl {
#[error(transparent)]
Io(#[from] std::io::Error),
#[cfg(feature = "reqwest")]
#[error(transparent)]
Reqwest(#[from] reqwest::Error),
#[error(transparent)]
TomlDe(#[from] toml::de::Error),
#[error(transparent)]
SerdeDe(#[from] serde_json::Error),
#[error(transparent)]
Parse(#[from] crate::SourceParseError),
}
impl ErrorImpl {
fn into_error<T>(value: T) -> Error
where
ErrorImpl: From<T>,
{
let inner = ErrorImpl::from(value);
let kind = match &inner {
Self::Io(_) => ErrorKind::Io,
#[cfg(feature = "reqwest")]
Self::Reqwest(_) => ErrorKind::Reqwest,
Self::TomlDe(_) => ErrorKind::TomlDe,
Self::SerdeDe(_) => ErrorKind::SerdeDe,
Self::Parse(_) => ErrorKind::Parse,
};
Error { kind, inner }
}
}
impl<T> From<T> for Error
where
ErrorImpl: From<T>,
{
fn from(err: T) -> Self {
ErrorImpl::into_error(err)
}
}