docsrs_cli/error/
classify.rs1use super::detail::ErrorDetail;
14use super::vocab::{InternalOp, IoCause, IoOp};
15use super::{ErrorKind, ValueSource};
16
17impl ErrorDetail {
18 pub fn kind(&self) -> ErrorKind {
23 match self {
24 Self::Empty { subject }
27 | Self::TooLong { subject, .. }
28 | Self::Invalid { subject, .. }
29 | Self::ControlCharacters { subject }
30 | Self::ContainsWhitespace { subject, .. } => {
31 if subject.is_operator_config() {
32 ErrorKind::Config
33 } else {
34 ErrorKind::InvalidInput
35 }
36 }
37 Self::MustBeAtLeastOneSecond { source, .. } | Self::AboveHardMaximum { source, .. } => {
38 match source {
39 ValueSource::CommandLine => ErrorKind::InvalidInput,
40 ValueSource::ConfigFile => ErrorKind::Config,
41 }
42 }
43
44 Self::ItemPathNoSegments
45 | Self::ItemPathMissingItemName
46 | Self::CrateRefMultipleAt
47 | Self::CrateRefEmptyName
48 | Self::CrateRefEmptyVersion
49 | Self::VersionBuildMetadata
50 | Self::VersionVPrefix
51 | Self::UnknownMatchMode { .. }
52 | Self::UnknownItemType { .. }
53 | Self::UnsupportedLang { .. }
54 | Self::InvalidFilterExpression { .. }
55 | Self::ModuleFilterUnsupported
56 | Self::PageBelowOne
57 | Self::PerPageOutOfRange { .. }
58 | Self::MemberKindNeedsParent { .. }
59 | Self::ItemPathSegmentCharset { .. }
60 | Self::ConflictingVersions { .. } => ErrorKind::InvalidInput,
61
62 Self::NotVisibleAscii { .. }
63 | Self::OriginBadScheme { .. }
64 | Self::OriginMissingHost { .. }
65 | Self::ConfigDirUnresolved
66 | Self::CacheDirUnresolved
67 | Self::ConfigTomlInvalid
68 | Self::ConfigTomlNotUtf8
69 | Self::ConfigTomlTooLarge { .. }
70 | Self::UserAgentHeaderInvalid
71 | Self::ConfigAlreadyExists { .. }
72 | Self::OriginNotAllowlisted { .. } => ErrorKind::Config,
73
74 Self::JsonFormatConflict
75 | Self::UnknownSchemaCommand { .. }
76 | Self::AmbientTargetRefused { .. }
77 | Self::ClapUsage { .. } => ErrorKind::Usage,
78
79 Self::HttpStatus { status, .. } => match status {
80 400 => ErrorKind::InvalidInput,
81 404 => ErrorKind::NotFound,
82 408 => ErrorKind::Timeout,
83 429 => ErrorKind::RateLimited,
84 500 | 502 | 503 | 504 => ErrorKind::Unavailable,
85 _ => ErrorKind::Network,
86 },
87
88 Self::HostNotAllowlisted { .. }
89 | Self::RedirectLimitExceeded
90 | Self::HttpClientBuild
91 | Self::BodyRead
92 | Self::BodyReserveFailed { .. } => ErrorKind::Network,
93
94 Self::BodyOverBudget { .. }
95 | Self::CachedBodyOverBudget { .. }
96 | Self::OutputOverBudget => ErrorKind::Budget,
97
98 Self::BodyNotUtf8
99 | Self::UnexpectedContentType { .. }
100 | Self::HtmlToMarkdown
101 | Self::CratesIoJson
102 | Self::HitJoinFailed
103 | Self::HitBaseInvalid => ErrorKind::Parse,
104
105 Self::AssocParentPageNotFound
106 | Self::AssocAnchorMissing { .. }
107 | Self::AssocAnchorEmpty { .. } => ErrorKind::NotFound,
108
109 Self::HttpRequestFailed { kind, .. } => *kind,
110
111 Self::Interrupted => ErrorKind::Interrupted,
112 Self::Terminated => ErrorKind::Terminated,
113 Self::BrokenPipe => ErrorKind::BrokenPipe,
114 Self::WallClockTimeout { .. } => ErrorKind::Timeout,
115
116 Self::Io { .. } => ErrorKind::Io,
119 Self::Internal { op } => match op {
120 InternalOp::SyntheticParseFailure => ErrorKind::Parse,
121 _ => ErrorKind::Internal,
122 },
123
124 Self::WithSuggestions { base, .. } => base.kind(),
126 }
127 }
128
129 pub fn io(op: IoOp, path: Option<String>, err: &std::io::Error) -> Self {
135 Self::Io {
136 op,
137 path,
138 cause: IoCause::of(err),
139 }
140 }
141
142 pub fn retryable(&self) -> bool {
148 match self {
149 Self::Io { cause, .. } => cause.retryable(),
150 Self::WithSuggestions { base, .. } => base.retryable(),
151 other => other.kind().retryable(),
152 }
153 }
154}