2017-09-07 23:00:08 +03:00
|
|
|
/*
|
|
|
|
* meli - error module
|
|
|
|
*
|
|
|
|
* Copyright 2017 Manos Pitsidianakis
|
|
|
|
*
|
|
|
|
* This file is part of meli.
|
|
|
|
*
|
|
|
|
* meli is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* meli is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with meli. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2018-08-06 22:20:34 +03:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* An error object for `melib`
|
|
|
|
*/
|
|
|
|
|
2018-08-11 18:00:21 +03:00
|
|
|
use std::borrow::Cow;
|
2017-09-01 15:24:32 +03:00
|
|
|
use std::error::Error;
|
|
|
|
use std::fmt;
|
|
|
|
use std::io;
|
2018-07-27 21:37:56 +03:00
|
|
|
use std::result;
|
2019-03-26 15:26:09 +02:00
|
|
|
use std::str;
|
2019-03-26 19:53:39 +02:00
|
|
|
use std::string;
|
2020-05-09 14:29:32 +03:00
|
|
|
use std::sync::Arc;
|
2017-09-01 15:24:32 +03:00
|
|
|
|
2017-09-14 18:08:14 +03:00
|
|
|
use nom;
|
|
|
|
|
2017-09-01 15:24:32 +03:00
|
|
|
pub type Result<T> = result::Result<T, MeliError>;
|
|
|
|
|
2020-05-09 14:29:32 +03:00
|
|
|
#[derive(Debug, Clone)]
|
2017-09-01 15:24:32 +03:00
|
|
|
pub struct MeliError {
|
2020-01-07 12:55:27 +02:00
|
|
|
pub summary: Option<Cow<'static, str>>,
|
|
|
|
pub details: Cow<'static, str>,
|
2020-05-09 14:29:32 +03:00
|
|
|
pub source: Option<std::sync::Arc<dyn Error + Send + Sync + 'static>>,
|
2017-09-01 15:24:32 +03:00
|
|
|
}
|
|
|
|
|
2020-06-06 12:27:02 +03:00
|
|
|
pub trait IntoMeliError {
|
|
|
|
fn set_err_summary<M>(self, msg: M) -> MeliError
|
|
|
|
where
|
|
|
|
M: Into<Cow<'static, str>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait ResultIntoMeliError<T> {
|
|
|
|
fn chain_err_summary<M, F>(self, msg_fn: F) -> Result<T>
|
|
|
|
where
|
|
|
|
F: Fn() -> M,
|
|
|
|
M: Into<Cow<'static, str>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<I: Into<MeliError>> IntoMeliError for I {
|
|
|
|
#[inline]
|
|
|
|
fn set_err_summary<M>(self, msg: M) -> MeliError
|
|
|
|
where
|
|
|
|
M: Into<Cow<'static, str>>,
|
|
|
|
{
|
|
|
|
let err: MeliError = self.into();
|
|
|
|
err.set_summary(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, I: Into<MeliError>> ResultIntoMeliError<T> for std::result::Result<T, I> {
|
|
|
|
#[inline]
|
|
|
|
fn chain_err_summary<M, F>(self, msg_fn: F) -> Result<T>
|
|
|
|
where
|
|
|
|
F: Fn() -> M,
|
|
|
|
M: Into<Cow<'static, str>>,
|
|
|
|
{
|
|
|
|
self.map_err(|err| err.set_err_summary(msg_fn()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-01 15:24:32 +03:00
|
|
|
impl MeliError {
|
2017-09-16 15:05:28 +03:00
|
|
|
pub fn new<M>(msg: M) -> MeliError
|
|
|
|
where
|
2020-01-07 12:55:27 +02:00
|
|
|
M: Into<Cow<'static, str>>,
|
2017-09-16 15:05:28 +03:00
|
|
|
{
|
|
|
|
MeliError {
|
2019-12-03 13:27:00 +02:00
|
|
|
summary: None,
|
2017-09-16 15:05:28 +03:00
|
|
|
details: msg.into(),
|
2020-05-09 14:29:32 +03:00
|
|
|
source: None,
|
2017-09-16 15:05:28 +03:00
|
|
|
}
|
2017-09-01 15:24:32 +03:00
|
|
|
}
|
2019-12-03 13:27:00 +02:00
|
|
|
|
|
|
|
pub fn set_summary<M>(mut self, summary: M) -> MeliError
|
|
|
|
where
|
2020-01-07 12:55:27 +02:00
|
|
|
M: Into<Cow<'static, str>>,
|
2019-12-03 13:27:00 +02:00
|
|
|
{
|
|
|
|
self.summary = Some(summary.into());
|
|
|
|
self
|
|
|
|
}
|
2020-05-09 14:29:32 +03:00
|
|
|
|
|
|
|
pub fn set_source(
|
|
|
|
mut self,
|
|
|
|
new_val: Option<std::sync::Arc<dyn Error + Send + Sync + 'static>>,
|
|
|
|
) -> MeliError {
|
|
|
|
self.source = new_val;
|
|
|
|
self
|
|
|
|
}
|
2017-09-01 15:24:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for MeliError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2020-05-09 14:29:32 +03:00
|
|
|
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)
|
2017-09-01 15:24:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-29 19:37:02 +02:00
|
|
|
impl Into<String> for MeliError {
|
|
|
|
fn into(self) -> String {
|
2020-01-07 12:55:27 +02:00
|
|
|
self.details.into()
|
2019-03-29 19:37:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-01 15:24:32 +03:00
|
|
|
impl Error for MeliError {
|
2020-05-09 14:29:32 +03:00
|
|
|
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
|
|
|
self.source.as_ref().map(|s| &(*(*s)) as _)
|
2017-09-01 15:24:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<io::Error> for MeliError {
|
|
|
|
#[inline]
|
|
|
|
fn from(kind: io::Error) -> MeliError {
|
2020-05-09 14:29:32 +03:00
|
|
|
MeliError::new(kind.to_string()).set_source(Some(Arc::new(kind)))
|
2017-09-01 15:24:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 18:08:14 +03:00
|
|
|
impl From<nom::IError> for MeliError {
|
|
|
|
#[inline]
|
|
|
|
fn from(kind: nom::IError) -> MeliError {
|
|
|
|
MeliError::new(format!("{:?}", kind))
|
|
|
|
}
|
|
|
|
}
|
2018-08-06 11:05:09 +03:00
|
|
|
|
2018-08-08 10:41:25 +03:00
|
|
|
impl<'a> From<Cow<'a, str>> for MeliError {
|
|
|
|
#[inline]
|
|
|
|
fn from(kind: Cow<'_, str>) -> MeliError {
|
|
|
|
MeliError::new(format!("{:?}", kind))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-11 22:47:27 +03:00
|
|
|
impl From<string::FromUtf8Error> for MeliError {
|
|
|
|
#[inline]
|
|
|
|
fn from(kind: string::FromUtf8Error) -> MeliError {
|
2020-05-09 14:29:32 +03:00
|
|
|
MeliError::new(format!("{:?}", kind)).set_source(Some(Arc::new(kind)))
|
2018-08-11 22:47:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 15:26:09 +02:00
|
|
|
impl From<str::Utf8Error> for MeliError {
|
|
|
|
#[inline]
|
|
|
|
fn from(kind: str::Utf8Error) -> MeliError {
|
2020-05-09 14:29:32 +03:00
|
|
|
MeliError::new(format!("{:?}", kind)).set_source(Some(Arc::new(kind)))
|
2019-03-26 15:26:09 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-06 11:05:09 +03:00
|
|
|
//use std::option;
|
|
|
|
//impl From<option::NoneError> for MeliError {
|
|
|
|
// #[inline]
|
|
|
|
// fn from(kind: option::NoneError) -> MeliError {
|
|
|
|
// MeliError::new(format!("{:?}", kind))
|
|
|
|
// }
|
|
|
|
//}
|
2019-08-26 19:44:05 +03:00
|
|
|
|
|
|
|
impl<T> From<std::sync::PoisonError<T>> for MeliError {
|
|
|
|
#[inline]
|
|
|
|
fn from(kind: std::sync::PoisonError<T>) -> MeliError {
|
|
|
|
MeliError::new(format!("{}", kind))
|
|
|
|
}
|
|
|
|
}
|
2019-08-26 20:07:41 +03:00
|
|
|
|
2019-09-21 21:23:06 +03:00
|
|
|
#[cfg(feature = "imap_backend")]
|
2019-08-26 20:07:41 +03:00
|
|
|
impl From<native_tls::HandshakeError<std::net::TcpStream>> for MeliError {
|
|
|
|
#[inline]
|
|
|
|
fn from(kind: native_tls::HandshakeError<std::net::TcpStream>) -> MeliError {
|
2020-05-09 14:29:32 +03:00
|
|
|
MeliError::new(format!("{}", kind)).set_source(Some(Arc::new(kind)))
|
2019-08-26 20:07:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-21 21:23:06 +03:00
|
|
|
#[cfg(feature = "imap_backend")]
|
2019-08-26 20:07:41 +03:00
|
|
|
impl From<native_tls::Error> for MeliError {
|
|
|
|
#[inline]
|
|
|
|
fn from(kind: native_tls::Error) -> MeliError {
|
2020-05-09 14:29:32 +03:00
|
|
|
MeliError::new(format!("{}", kind)).set_source(Some(Arc::new(kind)))
|
2019-08-26 20:07:41 +03:00
|
|
|
}
|
|
|
|
}
|
2019-10-12 18:03:58 +03:00
|
|
|
|
2019-12-02 01:09:23 +02:00
|
|
|
impl From<std::num::ParseIntError> for MeliError {
|
|
|
|
#[inline]
|
|
|
|
fn from(kind: std::num::ParseIntError) -> MeliError {
|
2020-05-09 14:29:32 +03:00
|
|
|
MeliError::new(format!("{}", kind)).set_source(Some(Arc::new(kind)))
|
2019-12-02 01:09:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 13:25:49 +02:00
|
|
|
#[cfg(feature = "jmap_backend")]
|
|
|
|
impl From<reqwest::Error> for MeliError {
|
|
|
|
#[inline]
|
|
|
|
fn from(kind: reqwest::Error) -> MeliError {
|
2020-05-09 14:29:32 +03:00
|
|
|
MeliError::new(format!("{}", kind)).set_source(Some(Arc::new(kind)))
|
2019-12-03 13:25:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "jmap_backend")]
|
|
|
|
impl From<serde_json::error::Error> for MeliError {
|
|
|
|
#[inline]
|
|
|
|
fn from(kind: serde_json::error::Error) -> MeliError {
|
2020-05-09 14:29:32 +03:00
|
|
|
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()))
|
2019-12-03 13:25:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-06 18:46:38 +03:00
|
|
|
impl From<std::ffi::NulError> for MeliError {
|
|
|
|
#[inline]
|
|
|
|
fn from(kind: std::ffi::NulError) -> MeliError {
|
2020-05-09 14:29:32 +03:00
|
|
|
MeliError::new(format!("{}", kind)).set_source(Some(Arc::new(kind)))
|
2020-05-06 18:46:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-29 15:35:29 +03:00
|
|
|
impl From<nix::Error> for MeliError {
|
|
|
|
#[inline]
|
|
|
|
fn from(kind: nix::Error) -> MeliError {
|
|
|
|
MeliError::new(format!("{}", kind)).set_source(Some(Arc::new(kind)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-03 17:06:24 +03:00
|
|
|
impl From<libloading::Error> for MeliError {
|
|
|
|
#[inline]
|
|
|
|
fn from(kind: libloading::Error) -> MeliError {
|
|
|
|
MeliError::new(format!("{}", kind)).set_source(Some(Arc::new(kind)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-12 18:03:58 +03:00
|
|
|
impl From<&str> for MeliError {
|
|
|
|
#[inline]
|
|
|
|
fn from(kind: &str) -> MeliError {
|
|
|
|
MeliError::new(kind.to_string())
|
|
|
|
}
|
|
|
|
}
|
2020-01-19 14:43:36 +02:00
|
|
|
|
|
|
|
impl From<String> for MeliError {
|
|
|
|
#[inline]
|
|
|
|
fn from(kind: String) -> MeliError {
|
|
|
|
MeliError::new(kind)
|
|
|
|
}
|
|
|
|
}
|