diff --git a/Cargo.toml b/Cargo.toml index 986699cc..ea6d3c54 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,8 +16,8 @@ readme = "README.md" [dependencies] thiserror ="1.0" log = "0.4" -reqwest = {version = "0.11", features = ["blocking"]} -pprof = { version="0.6.2"} +reqwest = { version = "0.11", features = ["blocking"]} +pprof = "0.6.2" libc = "^0.2.66" [dev-dependencies] diff --git a/src/error.rs b/src/error.rs index dd3ff723..b16b6256 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,99 +1,61 @@ -use std::fmt; -use thiserror::Error; +// Copyright 2021 Developers of Pyroscope. + +// Licensed under the Apache License, Version 2.0 . This file may not be copied, modified, or distributed +// except according to those terms. /// Result Alias with PyroscopeError pub type Result = std::result::Result; /// Error type of Pyroscope -#[derive(Error, Debug)] -pub struct PyroscopeError { - pub msg: String, - source: Option>, -} +#[non_exhaustive] +#[derive(thiserror::Error, Debug)] +pub enum PyroscopeError { + #[error("Other: {}", &.0)] + AdHoc(String), -impl fmt::Display for PyroscopeError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.msg) - } -} + #[error("{msg}: {source:?}")] + Compat{ msg: String, #[source] source: Box }, -impl Default for PyroscopeError { - fn default() -> Self { - PyroscopeError { - msg: "".to_string(), - source: None, - } - } + #[error(transparent)] + Reqwest(#[from] reqwest::Error), + + #[error(transparent)] + Pprof(#[from] pprof::Error), + + #[error(transparent)] + TimeSource(#[from] std::time::SystemTimeError), + + #[error(transparent)] + Io(#[from] std::io::Error), } impl PyroscopeError { /// Create a new instance of PyroscopeError pub fn new(msg: &str) -> Self { - PyroscopeError { - msg: msg.to_string(), - source: None, - } + PyroscopeError::AdHoc(msg.to_string()) } /// Create a new instance of PyroscopeError with source - pub fn new_with_source(msg: &str, source: Box) -> Self { - PyroscopeError { + pub fn new_with_source(msg: &str, source: E) -> Self where E: std::error::Error + Send + Sync + 'static { + PyroscopeError::Compat { msg: msg.to_string(), - source: Some(source), - } - } -} - -impl From for PyroscopeError { - fn from(err: reqwest::Error) -> Self { - PyroscopeError { - msg: String::from("reqwest Error"), - source: Some(Box::new(err)), - } - } -} - -impl From for PyroscopeError { - fn from(err: pprof::Error) -> Self { - PyroscopeError { - msg: String::from("pprof Error"), - source: Some(Box::new(err)), - } - } -} - -impl From for PyroscopeError { - fn from(err: std::time::SystemTimeError) -> Self { - PyroscopeError { - msg: String::from("SystemTime Error"), - source: Some(Box::new(err)), - } - } -} - -impl From for PyroscopeError { - fn from(err: std::io::Error) -> Self { - PyroscopeError { - msg: String::from("IO Error"), - source: Some(Box::new(err)), + source: Box::new(source), } } } impl From> for PyroscopeError { fn from(_err: std::sync::PoisonError) -> Self { - PyroscopeError { - msg: String::from("Poison Error"), - source: None, - } + PyroscopeError::AdHoc("Poison Error".to_owned()) } } impl From> for PyroscopeError { fn from(err: std::sync::mpsc::SendError) -> Self { - PyroscopeError { + PyroscopeError::Compat { msg: String::from("SendError Error"), - source: Some(Box::new(err)), + source: Box::new(err), } } }