-
Notifications
You must be signed in to change notification settings - Fork 41
use more features of thiserror
#11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,99 +1,61 @@ | ||
| use std::fmt; | ||
| use thiserror::Error; | ||
| // Copyright 2021 Developers of Pyroscope. | ||
|
|
||
| // Licensed under the Apache License, Version 2.0 <LICENSE or | ||
| // https://www.apache.org/licenses/LICENSE-2.0>. This file may not be copied, modified, or distributed | ||
| // except according to those terms. | ||
|
|
||
| /// Result Alias with PyroscopeError | ||
| pub type Result<T> = std::result::Result<T, PyroscopeError>; | ||
|
|
||
| /// Error type of Pyroscope | ||
| #[derive(Error, Debug)] | ||
| pub struct PyroscopeError { | ||
| pub msg: String, | ||
| source: Option<Box<dyn std::error::Error + Send + Sync>>, | ||
| } | ||
| #[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<dyn std::error::Error + Send + Sync + 'static> }, | ||
|
|
||
| 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<dyn std::error::Error + Send + Sync>) -> Self { | ||
| PyroscopeError { | ||
| pub fn new_with_source<E>(msg: &str, source: E) -> Self where E: std::error::Error + Send + Sync + 'static { | ||
| PyroscopeError::Compat { | ||
| msg: msg.to_string(), | ||
| source: Some(source), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl From<reqwest::Error> for PyroscopeError { | ||
| 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 { | ||
| 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 { | ||
| 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 { | ||
| PyroscopeError { | ||
| msg: String::from("IO Error"), | ||
| source: Some(Box::new(err)), | ||
| source: Box::new(source), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<T> From<std::sync::PoisonError<T>> for PyroscopeError { | ||
| fn from(_err: std::sync::PoisonError<T>) -> Self { | ||
| PyroscopeError { | ||
| msg: String::from("Poison Error"), | ||
| source: None, | ||
| } | ||
| PyroscopeError::AdHoc("Poison Error".to_owned()) | ||
| } | ||
| } | ||
|
|
||
| impl<T: 'static + Send + Sync> From<std::sync::mpsc::SendError<T>> for PyroscopeError { | ||
| fn from(err: std::sync::mpsc::SendError<T>) -> Self { | ||
| PyroscopeError { | ||
| PyroscopeError::Compat { | ||
| msg: String::from("SendError Error"), | ||
| source: Some(Box::new(err)), | ||
| source: Box::new(err), | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.