|
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. |
3 | 6 |
|
4 | 7 | /// Result Alias with PyroscopeError |
5 | 8 | pub type Result<T> = std::result::Result<T, PyroscopeError>; |
6 | 9 |
|
7 | 10 | /// 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), |
13 | 16 |
|
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> }, |
19 | 19 |
|
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), |
27 | 31 | } |
28 | 32 |
|
29 | 33 | impl PyroscopeError { |
30 | 34 | /// Create a new instance of PyroscopeError |
31 | 35 | pub fn new(msg: &str) -> Self { |
32 | | - PyroscopeError { |
33 | | - msg: msg.to_string(), |
34 | | - source: None, |
35 | | - } |
| 36 | + PyroscopeError::AdHoc(msg.to_string()) |
36 | 37 | } |
37 | 38 |
|
38 | 39 | /// 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 { |
41 | 42 | 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), |
79 | 44 | } |
80 | 45 | } |
81 | 46 | } |
82 | 47 |
|
83 | 48 | impl<T> From<std::sync::PoisonError<T>> for PyroscopeError { |
84 | 49 | 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()) |
89 | 51 | } |
90 | 52 | } |
91 | 53 |
|
92 | 54 | impl<T: 'static + Send + Sync> From<std::sync::mpsc::SendError<T>> for PyroscopeError { |
93 | 55 | fn from(err: std::sync::mpsc::SendError<T>) -> Self { |
94 | | - PyroscopeError { |
| 56 | + PyroscopeError::Compat { |
95 | 57 | msg: String::from("SendError Error"), |
96 | | - source: Some(Box::new(err)), |
| 58 | + source: Box::new(err), |
97 | 59 | } |
98 | 60 | } |
99 | 61 | } |
0 commit comments