Skip to content

fix: show panic output when fullscreen app panic#114

Open
hsqStephenZhang wants to merge 2 commits intoccbrown:mainfrom
hsqStephenZhang:main
Open

fix: show panic output when fullscreen app panic#114
hsqStephenZhang wants to merge 2 commits intoccbrown:mainfrom
hsqStephenZhang:main

Conversation

@hsqStephenZhang
Copy link
Contributor

What It Does

setup panic handler before enter fullscreen mode
in the handler, leave current screen before printing the backtrace, so that panic output own't get lost along with that screen

Related Issues

#103

@codecov
Copy link

codecov bot commented Jul 22, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.58%. Comparing base (79d852a) to head (47066be).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #114   +/-   ##
=======================================
  Coverage   91.58%   91.58%           
=======================================
  Files          29       29           
  Lines        4715     4715           
  Branches     4715     4715           
=======================================
  Hits         4318     4318           
  Misses        309      309           
  Partials       88       88           

Impacted file tree graph

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ccbrown
Copy link
Owner

ccbrown commented Jul 23, 2025

Thanks for the PR! I like the idea, but I don't love exposing crossterm and requiring users to use it directly. If the stack is allowed to unwind, fullscreen will be exited normally, so I wonder if something like this would be better:

diff --git a/examples/fullscreen.rs b/examples/fullscreen.rs
index 41d4424..ebf87fb 100644
--- a/examples/fullscreen.rs
+++ b/examples/fullscreen.rs
@@ -1,6 +1,10 @@
 use chrono::Local;
 use iocraft::prelude::*;
-use std::time::Duration;
+use std::{backtrace::Backtrace, cell::Cell, time::Duration};
+
+thread_local! {
+    static BACKTRACE: Cell<Option<Backtrace>> = const { Cell::new(None) };
+}
 
 #[component]
 fn Example(mut hooks: Hooks) -> impl Into<AnyElement<'static>> {
@@ -21,6 +25,7 @@ fn Example(mut hooks: Hooks) -> impl Into<AnyElement<'static>> {
             TerminalEvent::Key(KeyEvent { code, kind, .. }) if kind != KeyEventKind::Release => {
                 match code {
                     KeyCode::Char('q') => should_exit.set(true),
+                    KeyCode::Char('p') => panic!("oh no!"),
                     _ => {}
                 }
             }
@@ -54,11 +59,23 @@ fn Example(mut hooks: Hooks) -> impl Into<AnyElement<'static>> {
             ) {
                 Text(content: format!("Current Time: {}", time.get().format("%r")))
             }
-            Text(content: "Press \"q\" to quit.")
+            Text(content: "Press \"q\" to quit or \"p\" to panic.")
         }
     }
 }
 
 fn main() {
-    smol::block_on(element!(Example).fullscreen()).unwrap();
+    std::panic::set_hook(Box::new(|_| {
+        let trace = Backtrace::capture();
+        BACKTRACE.with(move |b| b.set(Some(trace)));
+    }));
+
+    if std::panic::catch_unwind(|| {
+        smol::block_on(element!(Example).fullscreen()).unwrap();
+    })
+    .is_err()
+    {
+        let b = BACKTRACE.with(|b| b.take()).unwrap();
+        println!("panic:\n\n{b}");
+    }
 }

@hsqStephenZhang
Copy link
Contributor Author

yeah agreed, catching and print the panic explictly info a better approach without exposing crossterm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants