melib/error: impl From<io::ErrorKind> for ErrorKind

We inspect errors in the frontend to check for network errors. If the
network error comes from std::io, this would get converted to an Error
with description "timed out", kind OSError, and source the actual
networking error.

This commit converts network std::io::ErrorKinds into appropriate
native ErrorKinds.

Signed-off-by: Manos Pitsidianakis <manos@pitsidianak.is>
feature/async-job-delay-wait
Manos Pitsidianakis 10 months ago
parent dc2b00442b
commit b3858de2f4
No known key found for this signature in database
GPG Key ID: 7729C7707F7E09D0

@ -107,7 +107,7 @@ mod dbus {
Some(NotificationType::Error(melib::ErrorKind::Network(_))) => { Some(NotificationType::Error(melib::ErrorKind::Network(_))) => {
notification.icon("network-error"); notification.icon("network-error");
} }
Some(NotificationType::Error(melib::ErrorKind::Timeout)) => { Some(NotificationType::Error(melib::ErrorKind::TimedOut)) => {
notification.icon("network-offline"); notification.icon("network-offline");
} }
_ => {} _ => {}

@ -332,7 +332,7 @@ pub enum ErrorKind {
ProtocolNotSupported, ProtocolNotSupported,
Bug, Bug,
Network(NetworkErrorKind), Network(NetworkErrorKind),
Timeout, TimedOut,
OSError, OSError,
NotImplemented, NotImplemented,
NotSupported, NotSupported,
@ -353,7 +353,7 @@ impl std::fmt::Display for ErrorKind {
Self::ProtocolError => "Protocol error", Self::ProtocolError => "Protocol error",
Self::ProtocolNotSupported => Self::ProtocolNotSupported =>
"Protocol is not supported. It could be the wrong type or version.", "Protocol is not supported. It could be the wrong type or version.",
Self::Timeout => "Timeout", Self::TimedOut => "Timed Out",
Self::OSError => "OS Error", Self::OSError => "OS Error",
Self::Configuration => "Configuration", Self::Configuration => "Configuration",
Self::NotImplemented => "Not implemented", Self::NotImplemented => "Not implemented",
@ -385,7 +385,7 @@ impl ErrorKind {
is_variant! { is_oserror, OSError } is_variant! { is_oserror, OSError }
is_variant! { is_protocol_error, ProtocolError } is_variant! { is_protocol_error, ProtocolError }
is_variant! { is_protocol_not_supported, ProtocolNotSupported } is_variant! { is_protocol_not_supported, ProtocolNotSupported }
is_variant! { is_timeout, Timeout } is_variant! { is_timeout, TimedOut }
is_variant! { is_value_error, ValueError } is_variant! { is_value_error, ValueError }
} }
@ -534,13 +534,27 @@ impl std::error::Error for Error {
} }
} }
impl From<io::ErrorKind> for ErrorKind {
fn from(kind: io::ErrorKind) -> Self {
match kind {
io::ErrorKind::ConnectionRefused
| io::ErrorKind::ConnectionReset
| io::ErrorKind::ConnectionAborted
| io::ErrorKind::NotConnected => Self::Network(NetworkErrorKind::ConnectionFailed),
io::ErrorKind::TimedOut => Self::TimedOut,
_ => Self::OSError,
}
}
}
impl From<io::Error> for Error { impl From<io::Error> for Error {
#[inline] #[inline]
fn from(kind: io::Error) -> Self { fn from(err: io::Error) -> Self {
Self::new(kind.to_string()) let kind = err.kind().into();
.set_details(kind.kind().to_string()) Self::new(err.to_string())
.set_source(Some(Arc::new(kind))) .set_details(err.kind().to_string())
.set_kind(ErrorKind::OSError) .set_source(Some(Arc::new(err)))
.set_kind(kind)
} }
} }

@ -651,7 +651,7 @@ impl ImapConnection {
"Connection timed out after {} seconds", "Connection timed out after {} seconds",
IMAP_PROTOCOL_TIMEOUT.as_secs() IMAP_PROTOCOL_TIMEOUT.as_secs()
)) ))
.set_kind(ErrorKind::Timeout); .set_kind(ErrorKind::TimedOut);
*status = Err(err.clone()); *status = Err(err.clone());
self.stream = Err(err); self.stream = Err(err);
} }

@ -33,7 +33,7 @@ pub async fn timeout<O>(
Either::Left((out, _)) => Ok(out), Either::Left((out, _)) => Ok(out),
Either::Right(_) => { Either::Right(_) => {
Err(crate::error::Error::new("Timed out.") Err(crate::error::Error::new("Timed out.")
.set_kind(crate::error::ErrorKind::Timeout)) .set_kind(crate::error::ErrorKind::TimedOut))
} }
} }
} else { } else {

Loading…
Cancel
Save