Skip to content

Commit 055576f

Browse files
authored
Merge pull request #11 from drahnr/better-error
use more features of `thiserror`
2 parents 51a4f0e + dcea73b commit 055576f

File tree

2 files changed

+32
-70
lines changed

2 files changed

+32
-70
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ readme = "README.md"
1616
[dependencies]
1717
thiserror ="1.0"
1818
log = "0.4"
19-
reqwest = {version = "0.11", features = ["blocking"]}
20-
pprof = { version="0.6.2"}
19+
reqwest = { version = "0.11", features = ["blocking"]}
20+
pprof = "0.6.2"
2121
libc = "^0.2.66"
2222

2323
[dev-dependencies]

src/error.rs

Lines changed: 30 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,61 @@
1-
use std::fmt;
2-
use thiserror::Error;
1+
// Copyright 2021 Developers of Pyroscope.
2+
3+
// Licensed under the Apache License, Version 2.0 <LICENSE or
4+
// https://www.apache.org/licenses/LICENSE-2.0>. This file may not be copied, modified, or distributed
5+
// except according to those terms.
36

47
/// Result Alias with PyroscopeError
58
pub type Result<T> = std::result::Result<T, PyroscopeError>;
69

710
/// Error type of Pyroscope
8-
#[derive(Error, Debug)]
9-
pub struct PyroscopeError {
10-
pub msg: String,
11-
source: Option<Box<dyn std::error::Error + Send + Sync>>,
12-
}
11+
#[non_exhaustive]
12+
#[derive(thiserror::Error, Debug)]
13+
pub enum PyroscopeError {
14+
#[error("Other: {}", &.0)]
15+
AdHoc(String),
1316

14-
impl fmt::Display for PyroscopeError {
15-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
16-
write!(f, "{}", self.msg)
17-
}
18-
}
17+
#[error("{msg}: {source:?}")]
18+
Compat{ msg: String, #[source] source: Box<dyn std::error::Error + Send + Sync + 'static> },
1919

20-
impl Default for PyroscopeError {
21-
fn default() -> Self {
22-
PyroscopeError {
23-
msg: "".to_string(),
24-
source: None,
25-
}
26-
}
20+
#[error(transparent)]
21+
Reqwest(#[from] reqwest::Error),
22+
23+
#[error(transparent)]
24+
Pprof(#[from] pprof::Error),
25+
26+
#[error(transparent)]
27+
TimeSource(#[from] std::time::SystemTimeError),
28+
29+
#[error(transparent)]
30+
Io(#[from] std::io::Error),
2731
}
2832

2933
impl PyroscopeError {
3034
/// Create a new instance of PyroscopeError
3135
pub fn new(msg: &str) -> Self {
32-
PyroscopeError {
33-
msg: msg.to_string(),
34-
source: None,
35-
}
36+
PyroscopeError::AdHoc(msg.to_string())
3637
}
3738

3839
/// Create a new instance of PyroscopeError with source
39-
pub fn new_with_source(msg: &str, source: Box<dyn std::error::Error + Send + Sync>) -> Self {
40-
PyroscopeError {
40+
pub fn new_with_source<E>(msg: &str, source: E) -> Self where E: std::error::Error + Send + Sync + 'static {
41+
PyroscopeError::Compat {
4142
msg: msg.to_string(),
42-
source: Some(source),
43-
}
44-
}
45-
}
46-
47-
impl From<reqwest::Error> for PyroscopeError {
48-
fn from(err: reqwest::Error) -> Self {
49-
PyroscopeError {
50-
msg: String::from("reqwest Error"),
51-
source: Some(Box::new(err)),
52-
}
53-
}
54-
}
55-
56-
impl From<pprof::Error> for PyroscopeError {
57-
fn from(err: pprof::Error) -> Self {
58-
PyroscopeError {
59-
msg: String::from("pprof Error"),
60-
source: Some(Box::new(err)),
61-
}
62-
}
63-
}
64-
65-
impl From<std::time::SystemTimeError> for PyroscopeError {
66-
fn from(err: std::time::SystemTimeError) -> Self {
67-
PyroscopeError {
68-
msg: String::from("SystemTime Error"),
69-
source: Some(Box::new(err)),
70-
}
71-
}
72-
}
73-
74-
impl From<std::io::Error> for PyroscopeError {
75-
fn from(err: std::io::Error) -> Self {
76-
PyroscopeError {
77-
msg: String::from("IO Error"),
78-
source: Some(Box::new(err)),
43+
source: Box::new(source),
7944
}
8045
}
8146
}
8247

8348
impl<T> From<std::sync::PoisonError<T>> for PyroscopeError {
8449
fn from(_err: std::sync::PoisonError<T>) -> Self {
85-
PyroscopeError {
86-
msg: String::from("Poison Error"),
87-
source: None,
88-
}
50+
PyroscopeError::AdHoc("Poison Error".to_owned())
8951
}
9052
}
9153

9254
impl<T: 'static + Send + Sync> From<std::sync::mpsc::SendError<T>> for PyroscopeError {
9355
fn from(err: std::sync::mpsc::SendError<T>) -> Self {
94-
PyroscopeError {
56+
PyroscopeError::Compat {
9557
msg: String::from("SendError Error"),
96-
source: Some(Box::new(err)),
58+
source: Box::new(err),
9759
}
9860
}
9961
}

0 commit comments

Comments
 (0)