Skip to content

Commit

Permalink
imp(error): add source and default to PyroscopeError
Browse files Browse the repository at this point in the history
  • Loading branch information
omarabid committed Jan 21, 2022
1 parent 146c333 commit 74d2f8d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
29 changes: 17 additions & 12 deletions src/backends/pprof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ impl Backend for Pprof<'_> {
fn initialize(&mut self, sample_rate: i32) -> Result<()> {
// Check if Backend is Uninitialized
if self.state != State::Uninitialized {
return Err(crate::error::PyroscopeError {
msg: String::from("Pprof Backend is already Initialized"),
});
//return Err(crate::error::PyroscopeError {
//msg: String::from("Pprof Backend is already Initialized"),
//});
return Err(crate::error::PyroscopeError::default());
}

// Construct a ProfilerGuardBuilder
Expand All @@ -50,9 +51,10 @@ impl Backend for Pprof<'_> {
fn start(&mut self) -> Result<()> {
// Check if Backend is Ready
if self.state != State::Ready {
return Err(crate::error::PyroscopeError {
msg: String::from("Pprof Backend is not Ready"),
});
//return Err(crate::error::PyroscopeError {
//msg: String::from("Pprof Backend is not Ready"),
//});
return Err(crate::error::PyroscopeError::default());
}

self.guard = Some(self.inner_builder.as_ref().unwrap().clone().build()?);
Expand All @@ -66,9 +68,11 @@ impl Backend for Pprof<'_> {
fn stop(&mut self) -> Result<()> {
// Check if Backend is Running
if self.state != State::Running {
return Err(crate::error::PyroscopeError {
msg: String::from("Pprof Backend is not Running"),
});
//return Err(crate::error::PyroscopeError {
//msg: String::from("Pprof Backend is not Running"),
//});

return Err(crate::error::PyroscopeError::default());
}

// drop the guard
Expand All @@ -83,9 +87,10 @@ impl Backend for Pprof<'_> {
fn report(&mut self) -> Result<Vec<u8>> {
// Check if Backend is Running
if self.state != State::Running {
return Err(crate::error::PyroscopeError {
msg: String::from("Pprof Backend is not Running"),
});
//return Err(crate::error::PyroscopeError {
//msg: String::from("Pprof Backend is not Running"),
//});
return Err(crate::error::PyroscopeError::default());
}

let mut buffer = Vec::new();
Expand Down
26 changes: 21 additions & 5 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ use thiserror::Error;
pub type Result<T> = std::result::Result<T, PyroscopeError>;

/// Error type of Pyroscope
#[derive(Error, Debug, PartialEq)]
#[derive(Error, Debug)]
pub struct PyroscopeError {
pub msg: String,
source: Option<Box<dyn std::error::Error + Send + Sync>>,
}

impl fmt::Display for PyroscopeError {
Expand All @@ -22,34 +23,47 @@ impl fmt::Display for PyroscopeError {
}
}

impl Default for PyroscopeError {
fn default() -> Self {
PyroscopeError {
msg: "".to_string(),
source: None,
}
}
}

impl From<reqwest::Error> for PyroscopeError {
fn from(_err: reqwest::Error) -> Self {
fn from(err: reqwest::Error) -> Self {
PyroscopeError {
msg: String::from("reqwest Error"),
source: Some(Box::new(err)),
}
}
}

impl From<pprof::Error> for PyroscopeError {
fn from(_err: pprof::Error) -> Self {
fn from(err: pprof::Error) -> Self {
PyroscopeError {
msg: String::from("pprof Error"),
source: Some(Box::new(err)),
}
}
}

impl From<std::time::SystemTimeError> for PyroscopeError {
fn from(_err: std::time::SystemTimeError) -> Self {
fn from(err: std::time::SystemTimeError) -> Self {
PyroscopeError {
msg: String::from("SystemTime Error"),
source: Some(Box::new(err)),
}
}
}

impl From<std::io::Error> for PyroscopeError {
fn from(_err: std::io::Error) -> Self {
fn from(err: std::io::Error) -> Self {
PyroscopeError {
msg: String::from("IO Error"),
source: Some(Box::new(err)),
}
}
}
Expand All @@ -58,6 +72,7 @@ impl<T> From<std::sync::PoisonError<T>> for PyroscopeError {
fn from(_err: std::sync::PoisonError<T>) -> Self {
PyroscopeError {
msg: String::from("Poison/Mutex Error"),
source: None,
}
}
}
Expand All @@ -66,6 +81,7 @@ impl<T> From<std::sync::mpsc::SendError<T>> for PyroscopeError {
fn from(_err: std::sync::mpsc::SendError<T>) -> Self {
PyroscopeError {
msg: String::from("mpsc Send Error"),
source: None,
}
}
}

0 comments on commit 74d2f8d

Please sign in to comment.