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

PXP-635: [CLI] Terminate the TUI when there is a panic on TUI #174

Merged
merged 5 commits into from
Nov 23, 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
36 changes: 35 additions & 1 deletion cli/src/commands/tui/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use std::{io::stdout, sync::Arc, time::Duration};
use std::{
io::stdout,
panic::{self, PanicInfo},
sync::Arc,
time::Duration,
};

use crate::{
config::{ApiChannel, Config},
Expand All @@ -7,6 +12,7 @@ use crate::{
use crossterm::{
event::{DisableMouseCapture, EnableMouseCapture},
execute,
style::Print,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::{prelude::CrosstermBackend, widgets::ListState, Terminal};
Expand Down Expand Up @@ -91,6 +97,9 @@ pub async fn handle_tui(channel: ApiChannel) -> Result<bool, WKCliError> {

let arc_channel = Arc::new(channel);

// Set panic hook
panic::set_hook(Box::new(panic_hook));

tokio::spawn(async move {
while let Some(network_event) = receiver.recv().await {
let app = Arc::clone(&app);
Expand Down Expand Up @@ -180,3 +189,28 @@ pub async fn start_ui(app: &Arc<Mutex<App>>) -> std::io::Result<bool> {

Ok(true)
}

pub fn panic_hook(panic_info: &PanicInfo<'_>) {
let mut stdout = stdout();

let msg = match panic_info.payload().downcast_ref::<&'static str>() {
Some(s) => *s,
None => match panic_info.payload().downcast_ref::<String>() {
Some(s) => &s[..],
None => "Box<Any>",
},
Copy link
Contributor

@jk-gan jk-gan Nov 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does Box<Any> mean?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was originally taken from here

As my understanding was that its a placeholder to match the string return types

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok got it

};

let _ = disable_raw_mode();
let _ = execute!(stdout, DisableMouseCapture, LeaveAlternateScreen);

// Print stack trace. Must be done after!
if let Some(panic_info) = panic_info.location() {
let _ = execute!(
stdout,
Print(format!(
"thread '<unnamed>' panicked at '{msg}', {panic_info}",
)),
);
}
}
2 changes: 1 addition & 1 deletion cli/src/output/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ impl OutputTokenizer {
}
} else {
// this is more readable
#[allow(clippy::collapsible-else-if)]
#[allow(clippy::collapsible_else_if)]
if word.contains('(') {
let open_index = word.find('(').unwrap();
tokens.push(Token::Parentheses("(".to_string()));
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/services/vault/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ impl Default for VaultClient {
}

impl VaultClient {
pub const FETCH_SECRETS: &str = "/v1/secret/data";
pub const UPDATE_SECRET: &str = "/v1/secret/data";
pub const FETCH_SECRETS: &'static str = "/v1/secret/data";
pub const UPDATE_SECRET: &'static str = "/v1/secret/data";

pub fn new() -> Self {
let client = reqwest::Client::new();
Expand Down
Loading