Skip to content

Commit

Permalink
DRY constructors for RusticError
Browse files Browse the repository at this point in the history
Signed-off-by: simonsan <14062932+simonsan@users.noreply.github.com>
  • Loading branch information
simonsan committed Oct 30, 2024
1 parent be71df2 commit b8bd50d
Showing 1 changed file with 4 additions and 34 deletions.
38 changes: 4 additions & 34 deletions crates/core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,25 +277,10 @@ impl RusticError {
/// Creates a new error with the given kind and guidance.
pub fn with_source(
kind: ErrorKind,
guidance: impl Into<String>,
guidance: impl Into<SmolStr>,
source: impl Into<Box<(dyn std::error::Error + Send + Sync)>>,
) -> Box<Self> {
Box::new(Self {
kind,
guidance: guidance.into().into(),
context: Cow::default(),
source: Some(source.into()),
error_code: None,
docs_url: None,
new_issue_url: None,
existing_issue_url: None,
severity: None,
status: None,
ask_report: false,
// `Backtrace::capture()` will check if backtrace has been enabled
// internally. It's zero cost if backtrace is disabled.
backtrace: Some(Backtrace::capture()),
})
Self::new(kind, guidance).attach_source(source)
}

/// Checks if the error has a specific error code.
Expand All @@ -312,25 +297,10 @@ impl RusticError {

/// Creates a new error from a given error.
pub fn from<T: std::error::Error + Display + Send + Sync + 'static>(
error: T,
kind: ErrorKind,
error: T,
) -> Box<Self> {
Box::new(Self {
kind,
guidance: error.to_string().into(),
context: Cow::default(),
source: Some(Box::new(error)),
error_code: None,
docs_url: None,
new_issue_url: None,
existing_issue_url: None,
severity: None,
status: None,
ask_report: false,
// `Backtrace::capture()` will check if backtrace has been enabled
// internally. It's zero cost if backtrace is disabled.
backtrace: Some(Backtrace::capture()),
})
Self::with_source(kind, error.to_string(), error)
}
}

Expand Down

0 comments on commit b8bd50d

Please sign in to comment.