use super::detail::ErrorDetail;
use super::vocab::{InternalOp, IoCause, IoOp};
use super::{ErrorKind, ValueSource};
impl ErrorDetail {
pub fn kind(&self) -> ErrorKind {
match self {
Self::Empty { subject }
| Self::TooLong { subject, .. }
| Self::Invalid { subject, .. }
| Self::ControlCharacters { subject }
| Self::ContainsWhitespace { subject, .. } => {
if subject.is_operator_config() {
ErrorKind::Config
} else {
ErrorKind::InvalidInput
}
}
Self::MustBeAtLeastOneSecond { source, .. } | Self::AboveHardMaximum { source, .. } => {
match source {
ValueSource::CommandLine => ErrorKind::InvalidInput,
ValueSource::ConfigFile => ErrorKind::Config,
}
}
Self::ItemPathNoSegments
| Self::ItemPathMissingItemName
| Self::CrateRefMultipleAt
| Self::CrateRefEmptyName
| Self::CrateRefEmptyVersion
| Self::VersionBuildMetadata
| Self::VersionVPrefix
| Self::UnknownMatchMode { .. }
| Self::UnknownItemType { .. }
| Self::UnsupportedLang { .. }
| Self::InvalidFilterExpression { .. }
| Self::ModuleFilterUnsupported
| Self::PageBelowOne
| Self::PerPageOutOfRange { .. }
| Self::MemberKindNeedsParent { .. }
| Self::ItemPathSegmentCharset { .. }
| Self::ConflictingVersions { .. } => ErrorKind::InvalidInput,
Self::NotVisibleAscii { .. }
| Self::OriginBadScheme { .. }
| Self::OriginMissingHost { .. }
| Self::ConfigDirUnresolved
| Self::CacheDirUnresolved
| Self::ConfigTomlInvalid
| Self::ConfigTomlNotUtf8
| Self::ConfigTomlTooLarge { .. }
| Self::UserAgentHeaderInvalid
| Self::ConfigAlreadyExists { .. }
| Self::OriginNotAllowlisted { .. } => ErrorKind::Config,
Self::JsonFormatConflict
| Self::UnknownSchemaCommand { .. }
| Self::AmbientTargetRefused { .. }
| Self::ClapUsage { .. } => ErrorKind::Usage,
Self::HttpStatus { status, .. } => match status {
400 => ErrorKind::InvalidInput,
404 => ErrorKind::NotFound,
408 => ErrorKind::Timeout,
429 => ErrorKind::RateLimited,
500 | 502 | 503 | 504 => ErrorKind::Unavailable,
_ => ErrorKind::Network,
},
Self::HostNotAllowlisted { .. }
| Self::RedirectLimitExceeded
| Self::HttpClientBuild
| Self::BodyRead
| Self::BodyReserveFailed { .. } => ErrorKind::Network,
Self::BodyOverBudget { .. }
| Self::CachedBodyOverBudget { .. }
| Self::OutputOverBudget => ErrorKind::Budget,
Self::BodyNotUtf8
| Self::UnexpectedContentType { .. }
| Self::HtmlToMarkdown
| Self::CratesIoJson
| Self::HitJoinFailed
| Self::HitBaseInvalid => ErrorKind::Parse,
Self::AssocParentPageNotFound
| Self::AssocAnchorMissing { .. }
| Self::AssocAnchorEmpty { .. } => ErrorKind::NotFound,
Self::HttpRequestFailed { kind, .. } => *kind,
Self::Interrupted => ErrorKind::Interrupted,
Self::Terminated => ErrorKind::Terminated,
Self::BrokenPipe => ErrorKind::BrokenPipe,
Self::WallClockTimeout { .. } => ErrorKind::Timeout,
Self::Io { .. } => ErrorKind::Io,
Self::Internal { op } => match op {
InternalOp::SyntheticParseFailure => ErrorKind::Parse,
_ => ErrorKind::Internal,
},
Self::WithSuggestions { base, .. } => base.kind(),
}
}
pub fn io(op: IoOp, path: Option<String>, err: &std::io::Error) -> Self {
Self::Io {
op,
path,
cause: IoCause::of(err),
}
}
pub fn retryable(&self) -> bool {
match self {
Self::Io { cause, .. } => cause.retryable(),
Self::WithSuggestions { base, .. } => base.retryable(),
other => other.kind().retryable(),
}
}
}