errors: add `source` field to MeliError

async
Manos Pitsidianakis 2020-05-09 14:29:32 +03:00
parent d915c4a7c8
commit 3ea1ce5454
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
3 changed files with 42 additions and 16 deletions

View File

@ -30,15 +30,17 @@ use std::io;
use std::result;
use std::str;
use std::string;
use std::sync::Arc;
use nom;
pub type Result<T> = result::Result<T, MeliError>;
#[derive(Debug, Clone, Deserialize, Serialize)]
#[derive(Debug, Clone)]
pub struct MeliError {
pub summary: Option<Cow<'static, str>>,
pub details: Cow<'static, str>,
pub source: Option<std::sync::Arc<dyn Error + Send + Sync + 'static>>,
}
impl MeliError {
@ -49,6 +51,7 @@ impl MeliError {
MeliError {
summary: None,
details: msg.into(),
source: None,
}
}
@ -59,11 +62,26 @@ impl MeliError {
self.summary = Some(summary.into());
self
}
pub fn set_source(
mut self,
new_val: Option<std::sync::Arc<dyn Error + Send + Sync + 'static>>,
) -> MeliError {
self.source = new_val;
self
}
}
impl fmt::Display for MeliError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.details)
if let Some(summary) = self.summary.as_ref() {
write!(f, "Summary: {}\n", summary)?;
}
let ret = write!(f, "{}", self.details)?;
if let Some(source) = self.source.as_ref() {
write!(f, "\nCaused by: {}", source)?;
}
Ok(ret)
}
}
@ -74,15 +92,15 @@ impl Into<String> for MeliError {
}
impl Error for MeliError {
fn description(&self) -> &str {
&self.details
fn source(&self) -> Option<&(dyn Error + 'static)> {
self.source.as_ref().map(|s| &(*(*s)) as _)
}
}
impl From<io::Error> for MeliError {
#[inline]
fn from(kind: io::Error) -> MeliError {
MeliError::new(kind.description().to_string())
MeliError::new(kind.to_string()).set_source(Some(Arc::new(kind)))
}
}
@ -103,14 +121,14 @@ impl<'a> From<Cow<'a, str>> for MeliError {
impl From<string::FromUtf8Error> for MeliError {
#[inline]
fn from(kind: string::FromUtf8Error) -> MeliError {
MeliError::new(format!("{:?}", kind))
MeliError::new(format!("{:?}", kind)).set_source(Some(Arc::new(kind)))
}
}
impl From<str::Utf8Error> for MeliError {
#[inline]
fn from(kind: str::Utf8Error) -> MeliError {
MeliError::new(format!("{:?}", kind))
MeliError::new(format!("{:?}", kind)).set_source(Some(Arc::new(kind)))
}
}
//use std::option;
@ -132,7 +150,7 @@ impl<T> From<std::sync::PoisonError<T>> for MeliError {
impl From<native_tls::HandshakeError<std::net::TcpStream>> for MeliError {
#[inline]
fn from(kind: native_tls::HandshakeError<std::net::TcpStream>) -> MeliError {
MeliError::new(format!("{}", kind))
MeliError::new(format!("{}", kind)).set_source(Some(Arc::new(kind)))
}
}
@ -140,14 +158,14 @@ impl From<native_tls::HandshakeError<std::net::TcpStream>> for MeliError {
impl From<native_tls::Error> for MeliError {
#[inline]
fn from(kind: native_tls::Error) -> MeliError {
MeliError::new(format!("{}", kind))
MeliError::new(format!("{}", kind)).set_source(Some(Arc::new(kind)))
}
}
impl From<std::num::ParseIntError> for MeliError {
#[inline]
fn from(kind: std::num::ParseIntError) -> MeliError {
MeliError::new(format!("{}", kind))
MeliError::new(format!("{}", kind)).set_source(Some(Arc::new(kind)))
}
}
@ -155,7 +173,7 @@ impl From<std::num::ParseIntError> for MeliError {
impl From<reqwest::Error> for MeliError {
#[inline]
fn from(kind: reqwest::Error) -> MeliError {
MeliError::new(format!("{}", kind))
MeliError::new(format!("{}", kind)).set_source(Some(Arc::new(kind)))
}
}
@ -163,14 +181,21 @@ impl From<reqwest::Error> for MeliError {
impl From<serde_json::error::Error> for MeliError {
#[inline]
fn from(kind: serde_json::error::Error) -> MeliError {
MeliError::new(format!("{}", kind))
MeliError::new(format!("{}", kind)).set_source(Some(Arc::new(kind)))
}
}
impl From<Box<dyn Error + Sync + Send + 'static>> for MeliError {
#[inline]
fn from(kind: Box<dyn Error + Sync + Send + 'static>) -> MeliError {
MeliError::new(format!("{}", kind)).set_source(Some(kind.into()))
}
}
impl From<std::ffi::NulError> for MeliError {
#[inline]
fn from(kind: std::ffi::NulError) -> MeliError {
MeliError::new(format!("{}", kind))
MeliError::new(format!("{}", kind)).set_source(Some(Arc::new(kind)))
}
}

View File

@ -1333,8 +1333,9 @@ pub fn save_draft(
flags: Flag,
account_cursor: usize,
) {
if let Err(MeliError { summary, details }) =
context.accounts[account_cursor].save_special(bytes, mailbox_type, flags)
if let Err(MeliError {
summary, details, ..
}) = context.accounts[account_cursor].save_special(bytes, mailbox_type, flags)
{
context.replies.push_back(UIEvent::Notification(
summary.map(|s| s.into()),

View File

@ -51,7 +51,7 @@ use std::sync::{Arc, RwLock};
pub type Worker = Option<Async<Result<Vec<Envelope>>>>;
#[derive(Serialize, Debug)]
#[derive(Debug)]
pub enum MailboxStatus {
Available,
Failed(MeliError),