Skip to content
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

Make backtrace and color-eyre optional #860

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ rust-version = "1.60"

[dependencies]
abscissa_derive = { version = "0.7", path = "../derive" }
backtrace = "0.3"
canonical-path = "2"
fs-err = "2"
once_cell = "1.17"

# optional dependencies
arc-swap = { version = "1", optional = true }
backtrace = { version = "0.3", optional = true }
color-eyre = { version = "0.6", optional = true, default-features = false }
clap = { version = "4", optional = true, features = ["derive"] }
regex = { version = "1", optional = true }
Expand All @@ -46,6 +46,7 @@ features = ["fmt", "env-filter", "ansi", "smallvec", "tracing-log"]
[features]
default = [
"application",
"backtrace",
"secrets",
"testing",
]
Expand All @@ -57,6 +58,7 @@ application = [
"semver/serde",
"terminal"
]
backtrace = ["dep:color-eyre", "dep:backtrace"]
config = [
"secrets",
"serde",
Expand All @@ -65,7 +67,7 @@ config = [
]
options = ["clap"]
secrets = ["secrecy"]
terminal = ["color-eyre", "termcolor"]
terminal = ["termcolor"]
testing = ["regex", "wait-timeout"]
trace = [
"tracing",
Expand Down
6 changes: 6 additions & 0 deletions core/src/error/context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Error contexts

use super::BoxError;
#[cfg(feature = "backtrace")]
use backtrace::Backtrace;
use std::fmt::{self, Debug, Display};

Expand All @@ -14,6 +15,7 @@ where
kind: Kind,

/// Backtrace where error occurred
#[cfg(feature = "backtrace")]
backtrace: Option<Backtrace>,

/// Source of the error
Expand All @@ -26,9 +28,11 @@ where
{
/// Create a new error context
pub fn new(kind: Kind, source: Option<BoxError>) -> Self {
#[cfg(feature = "backtrace")]
let backtrace = Some(Backtrace::new_unresolved());
Context {
kind,
#[cfg(feature = "backtrace")]
backtrace,
source,
}
Expand All @@ -40,11 +44,13 @@ where
}

/// Get the backtrace associated with this error (if available)
#[cfg(feature = "backtrace")]
pub fn backtrace(&self) -> Option<&Backtrace> {
self.backtrace.as_ref()
}

/// Extract the backtrace from the context, allowing it to be resolved.
#[cfg(feature = "backtrace")]
pub fn into_backtrace(self) -> Option<Backtrace> {
self.backtrace
}
Expand Down
1 change: 1 addition & 0 deletions core/src/terminal/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ impl Terminal {
// TODO(tarcieri): handle terminal reinit (without panicking)
super::init(color_choice);

#[cfg(feature = "backtrace")]
if color_choice != ColorChoice::Never {
// TODO(tarcieri): avoid panicking here
color_eyre::install().expect("couldn't install color-eyre");
Expand Down