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

Add error panel, panic hook, and updated dependencies #1

Merged
merged 1 commit into from
Jun 21, 2023
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
68 changes: 34 additions & 34 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ edition = "2021"

[dependencies]
clap = { version = "4.3.0", features = ["std", "derive", "help", "usage", "suggestions"], default-features = false }
ureq = { version = "2.6.2", features = ["json", "tls"], default-features = false }
ureq = { version = "2.7.1", features = ["json", "tls"], default-features = false }
serde = { version = "1.0", features = ["derive"], default-features = false }
serde_with = { version = "2.2.0", default-features = false }
serde_with = { version = "3.0.0", default-features = false }
serde_json = { version = "1.0", default-features = false }
tabled = { version = "0.10", features = ["color"], default-features = false }
owo-colors = { version = "3.5.0", default-features = false }
json_to_table = { version = "*", default-features = false }
base64 = { version = "0.21.0", features = ["std"], default-features = false }
rustls = { version = "0.20.8", features = ["dangerous_configuration"], default-features = false }
rustls = { version = "0.21.2", features = ["dangerous_configuration"], default-features = false }
colored_json = { version = "3", default-features = false }
chrono = { version = "0.4.23", features = ["std", "default"], default-features = false }
chrono = { version = "0.4.26", features = ["std", "default"], default-features = false }
humansize = { version = "2.1.3", default-features = false }
humantime = { version = "2.1.0" }
tui = { version = "0.19.0", features = ["crossterm"] }
Expand Down
14 changes: 10 additions & 4 deletions src/cli/commands/view/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ pub(crate) struct App<'a> {
pub should_quit: bool,
pub connected: bool,
pub show_help: bool,
pub show_error: Option<String>,
pub tabs: TabsState,
pub pipelines: StatefulTable<PipelineItem>,
pub selected_pipeline_vertex: StatefulTable<String>,
Expand Down Expand Up @@ -319,6 +320,7 @@ impl<'a> App<'a> {
show_selected_pipeline_charts: false,
show_selected_vertex_details: false,
show_help: false,
show_error: None,
should_quit: false,
connected: false,
tabs: TabsState::new(),
Expand Down Expand Up @@ -433,24 +435,28 @@ impl<'a> App<'a> {

pub fn on_tick(&mut self) {
match self.data_fetcher.fetch_info() {
Ok(stats) => {
self.state.node_info = Some(stats);
Ok(info) => {
self.state.node_info = Some(info);
self.connected = true;
self.show_error = None;
}
Err(_) => {
Err(err) => {
self.state.node_info = None;
self.connected = false;
self.show_error = Some(err.to_string());
}
};

match self.data_fetcher.fetch_stats() {
Ok(stats) => {
self.state.node_stats = Some(stats);
self.connected = true;
self.show_error = None;
}
Err(_) => {
Err(err) => {
self.state.node_stats = None;
self.connected = false;
self.show_error = Some(err.to_string());
}
};

Expand Down
32 changes: 29 additions & 3 deletions src/cli/commands/view/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::commands::view::node_charts::render_node_charts;
use crate::commands::view::pipeline_view;

pub(crate) fn draw<B: Backend>(f: &mut Frame<B>, app: &mut App) {
let constraints = if app.show_help {
let constraints = if app.show_help || app.show_error.is_some() {
vec![
Constraint::Length(3),
Constraint::Min(0),
Expand Down Expand Up @@ -109,7 +109,7 @@ pub(crate) fn draw<B: Backend>(f: &mut Frame<B>, app: &mut App) {
Span::styled(" @ ", Style::default().fg(Color::Gray)),
Span::from(app.host),
Span::styled(
format!(" | Sampling every {}ms", app.refresh_interval.as_millis()),
format!(" | Sampling every {}s", app.refresh_interval.as_secs()),
Style::default().fg(Color::Gray),
),
])];
Expand All @@ -128,11 +128,37 @@ pub(crate) fn draw<B: Backend>(f: &mut Frame<B>, app: &mut App) {
};
}

if app.show_help {
if app.show_error.is_some() {
draw_error_panel(f, app, chunks[2]);
} else if app.show_help {
draw_help_panel(f, chunks[2]);
}
}

fn draw_error_panel<B>(f: &mut Frame<B>, app: &mut App, area: Rect)
where
B: Backend,
{
if let Some(error) = &app.show_error {
f.render_widget(Block::default().borders(Borders::ALL), area);

let footer_chunks = Layout::default()
.constraints([Constraint::Percentage(100)])
.direction(Direction::Horizontal)
.margin(1)
.split(area);

let w = Paragraph::new(vec![Spans::from(vec![
Span::styled("Error: ", Style::default().fg(Color::Red)),
Span::styled(error, Style::default().fg(Color::DarkGray)),
])])
.alignment(Alignment::Left)
.wrap(Wrap { trim: true });

f.render_widget(w, footer_chunks[0]);
}
}

fn draw_help_panel<B>(f: &mut Frame<B>, area: Rect)
where
B: Backend,
Expand Down
27 changes: 27 additions & 0 deletions src/cli/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::backtrace::Backtrace;
use std::panic;

use crate::config::Config;
use crate::errors::AnyError;
use crate::output::Output;
Expand Down Expand Up @@ -27,6 +30,8 @@ fn run() -> Result<ExitCode, AnyError> {
}

fn main() {
setup_panic_hook();

let result = run();
match result {
Err(err) => {
Expand All @@ -38,3 +43,25 @@ fn main() {
}
}
}

fn setup_panic_hook() {
panic::set_hook(Box::new(move |panic_info| {
let backtrace = Backtrace::force_capture().to_string();
let loc = if let Some(location) = panic_info.location() {
format!(" in file {} at line {}", location.file(), location.line())
} else {
String::new()
};

let message = if let Some(value) = panic_info.payload().downcast_ref::<&str>() {
value
} else if let Some(value) = panic_info.payload().downcast_ref::<String>() {
value
} else {
"Payload not captured as it is not a string."
};

println!("Panic occurred{} with the message: {}", loc, message);
println!("Panic backtrace: \n{}", backtrace);
}));
}
Loading