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

feat(console) error handling improvements #365

Merged
merged 4 commits into from
Aug 5, 2022
Merged
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
36 changes: 36 additions & 0 deletions tokio-console/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,42 @@ impl Config {
.clone()
}

pub(crate) fn add_issue_metadata(
&self,
mut builder: color_eyre::config::HookBuilder,
) -> color_eyre::config::HookBuilder {
macro_rules! add_issue_metadata {
($self:ident, $builder:ident =>
$(
$($name:ident).+
),+
$(,)?
) => {
$(
$builder = $builder.add_issue_metadata(concat!("config", $(".", stringify!($name)),+), format!("`{:?}`", $self$(.$name)+));
)*
}
}

add_issue_metadata! {
self, builder =>
subcmd,
target_addr,
env_filter,
log_directory,
retain_for,
view_options.no_colors,
view_options.lang,
view_options.ascii_only,
view_options.truecolor,
view_options.palette,
view_options.toggles.color_durations,
view_options.toggles.color_terminated,
}

builder
}

fn from_path(config_path: ConfigPath) -> color_eyre::Result<Option<Self>> {
ConfigFile::from_path(config_path)?
.map(|config| config.try_into())
Expand Down
15 changes: 8 additions & 7 deletions tokio-console/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ mod warnings;
#[tokio::main]
async fn main() -> color_eyre::Result<()> {
let mut args = config::Config::parse()?;
// initialize error handling first, in case panics occur while setting up
// other stuff.
let styles = view::Styles::from_config(args.view_options.clone());
styles.error_init(&args)?;

args.trace_init()?;
tracing::debug!(?args.target_addr, ?args.view_options);

match args.subcmd {
Some(config::OptionalCmd::GenConfig) => {
Expand All @@ -40,16 +47,10 @@ async fn main() -> color_eyre::Result<()> {
None => {}
}

let retain_for = args.retain_for();
args.trace_init()?;
tracing::debug!(?args.target_addr, ?args.view_options);

let target = args.target_addr();
tracing::info!(?target, "using target addr");

let styles = view::Styles::from_config(args.view_options);
styles.error_init()?;

let retain_for = args.retain_for();
let (mut terminal, _cleanup) = term::init_crossterm()?;
terminal.clear()?;
let mut conn = conn::Connection::new(target);
Expand Down
24 changes: 22 additions & 2 deletions tokio-console/src/view/styles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,32 @@ impl Styles {
}
}

pub fn error_init(&self) -> color_eyre::Result<()> {
use color_eyre::config::{HookBuilder, Theme};
pub fn error_init(&self, cfg: &crate::config::Config) -> color_eyre::Result<()> {
use color_eyre::{
config::{HookBuilder, Theme},
ErrorKind,
};

let mut builder = HookBuilder::new()
.issue_url(concat!(env!("CARGO_PKG_REPOSITORY"), "/issues/new"))
.issue_filter(|kind| match kind {
// Only suggest reporting GitHub issues for panics, not for
// errors, so people don't open GitHub issues for stuff like not
// being able to find a config file or connections being
// terminated by remote hosts.
ErrorKind::NonRecoverable(_) => true,
ErrorKind::Recoverable(_) => false,
})
// filter out `color-eyre`'s default set of frames to skip from
// backtraces.
//
// this includes `std::rt`, `color_eyre`'s own frames, and
// `tokio::runtime` & friends.
.add_default_filters()
.add_issue_metadata("version", env!("CARGO_PKG_VERSION"));
// Add all the config values to the GitHub issue metadata
builder = cfg.add_issue_metadata(builder);

if self.palette == Palette::NoColors {
// disable colors in error reports
builder = builder.theme(Theme::new());
Expand Down