Skip to content

Commit e1e007c

Browse files
committed
refactor(error): various minor changes
1 parent f8f96c0 commit e1e007c

File tree

3 files changed

+40
-23
lines changed

3 files changed

+40
-23
lines changed

examples/error.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,16 @@ fn fibonacci(n: u64) -> u64 {
1616
}
1717

1818
fn main() -> Result<()> {
19+
// Force rustc to display the log messages in the console.
20+
std::env::set_var("RUST_LOG", "trace");
21+
22+
// Initialize the logger.
23+
pretty_env_logger::init_timed();
24+
1925
// This example should fail and return an error.
26+
println!("This example should fail and return an error.");
27+
println!("Run this with: RUST_BACKTRACE=1 cargo run --example error");
28+
2029
let mut agent = PyroscopeAgent::builder("http://invalid_url", "example.error")
2130
.build()?;
2231
// Start Agent

src/backends/pprof.rs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use pprof::{ProfilerGuard, ProfilerGuardBuilder, Report};
88

99
use crate::backends::Backend;
1010
use crate::backends::State;
11+
use crate::PyroscopeError;
1112
use crate::Result;
1213

1314
#[derive(Default)]
@@ -31,10 +32,7 @@ impl Backend for Pprof<'_> {
3132
fn initialize(&mut self, sample_rate: i32) -> Result<()> {
3233
// Check if Backend is Uninitialized
3334
if self.state != State::Uninitialized {
34-
//return Err(crate::error::PyroscopeError {
35-
//msg: String::from("Pprof Backend is already Initialized"),
36-
//});
37-
return Err(crate::error::PyroscopeError::default());
35+
return Err(PyroscopeError::new("Pprof Backend is already Initialized"));
3836
}
3937

4038
// Construct a ProfilerGuardBuilder
@@ -51,10 +49,7 @@ impl Backend for Pprof<'_> {
5149
fn start(&mut self) -> Result<()> {
5250
// Check if Backend is Ready
5351
if self.state != State::Ready {
54-
//return Err(crate::error::PyroscopeError {
55-
//msg: String::from("Pprof Backend is not Ready"),
56-
//});
57-
return Err(crate::error::PyroscopeError::default());
52+
return Err(PyroscopeError::new("Pprof Backend is not Ready"));
5853
}
5954

6055
self.guard = Some(self.inner_builder.as_ref().unwrap().clone().build()?);
@@ -68,11 +63,7 @@ impl Backend for Pprof<'_> {
6863
fn stop(&mut self) -> Result<()> {
6964
// Check if Backend is Running
7065
if self.state != State::Running {
71-
//return Err(crate::error::PyroscopeError {
72-
//msg: String::from("Pprof Backend is not Running"),
73-
//});
74-
75-
return Err(crate::error::PyroscopeError::default());
66+
return Err(PyroscopeError::new("Pprof Backend is not Running"));
7667
}
7768

7869
// drop the guard
@@ -87,10 +78,7 @@ impl Backend for Pprof<'_> {
8778
fn report(&mut self) -> Result<Vec<u8>> {
8879
// Check if Backend is Running
8980
if self.state != State::Running {
90-
//return Err(crate::error::PyroscopeError {
91-
//msg: String::from("Pprof Backend is not Running"),
92-
//});
93-
return Err(crate::error::PyroscopeError::default());
81+
return Err(PyroscopeError::new("Pprof Backend is not Running"));
9482
}
9583

9684
let mut buffer = Vec::new();
@@ -107,7 +95,9 @@ impl Backend for Pprof<'_> {
10795

10896
// Copyright: https://github.com/YangKeao
10997
fn fold<W>(report: &Report, with_thread_name: bool, mut writer: W) -> Result<()>
110-
where W: std::io::Write {
98+
where
99+
W: std::io::Write,
100+
{
111101
for (key, value) in report.data.iter() {
112102
if with_thread_name {
113103
if !key.thread_name.is_empty() {

src/error.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@ impl Default for PyroscopeError {
3232
}
3333
}
3434

35+
impl PyroscopeError {
36+
/// Create a new instance of PyroscopeError
37+
pub fn new(msg: &str) -> Self {
38+
PyroscopeError {
39+
msg: msg.to_string(),
40+
source: None,
41+
}
42+
}
43+
44+
/// Create a new instance of PyroscopeError with source
45+
pub fn new_with_source(msg: &str, source: Box<dyn std::error::Error + Send + Sync>) -> Self {
46+
PyroscopeError {
47+
msg: msg.to_string(),
48+
source: Some(source),
49+
}
50+
}
51+
}
52+
3553
impl From<reqwest::Error> for PyroscopeError {
3654
fn from(err: reqwest::Error) -> Self {
3755
PyroscopeError {
@@ -71,17 +89,17 @@ impl From<std::io::Error> for PyroscopeError {
7189
impl<T> From<std::sync::PoisonError<T>> for PyroscopeError {
7290
fn from(_err: std::sync::PoisonError<T>) -> Self {
7391
PyroscopeError {
74-
msg: String::from("Poison/Mutex Error"),
92+
msg: String::from("Poison Error"),
7593
source: None,
7694
}
7795
}
7896
}
7997

80-
impl<T> From<std::sync::mpsc::SendError<T>> for PyroscopeError {
81-
fn from(_err: std::sync::mpsc::SendError<T>) -> Self {
98+
impl<T: 'static + Send + Sync> From<std::sync::mpsc::SendError<T>> for PyroscopeError {
99+
fn from(err: std::sync::mpsc::SendError<T>) -> Self {
82100
PyroscopeError {
83-
msg: String::from("mpsc Send Error"),
84-
source: None,
101+
msg: String::from("SendError Error"),
102+
source: Some(Box::new(err)),
85103
}
86104
}
87105
}

0 commit comments

Comments
 (0)